📄 faqcat38c2.html
字号:
<font color=blue>·</font><a href="../../misc/circshift.html"><!-- qtag -->Question 20.21a</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Does C have circular shift operators?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>No.(Part of the reason why is that the sizes of C's types aren'tprecisely defined--see question <a href="faqcatd3c2.html?sec=decl#exactsizes">1.2</a>--buta circular shift makes most sense when applied to a word of aparticular known size.)</p><p>You can implement a circular shiftusing two regular shifts and a bitwise OR:<pre> (x << 13) | (x >> 3) /* circular shift left 13 in 16 bits */</pre><hr><hr><hr><a name="quintplus"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/quintplus.html"><!-- qtag -->Question 20.21b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is C a great language, or what?Where else could you write something like<TT>a+++++b</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Well, you can'tmeaningfullywrite it in C, either.The rule for lexical analysisis thatat each point during a straightforward left-to-right scan,the longest possible token is determined,without regard to whether theresulting sequence of tokens makes sense.The fragment in the question is thereforeinterpreted as<pre>a ++ ++ + b</pre>andcannot be parsed as a valid expression.</p><p>References:K&R1 Sec. A2 p. 179<br>K&R2 Sec. A2.1 p. 192<br>ISO Sec. 6.1<br>H&S Sec. 2.3 pp. 19-20<hr><hr><hr><a name="colonequal"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/colonequal.html"><!-- qtag -->Question 20.22</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>If the assignment operator were <TT>:=</TT>,wouldn't it then be harder to accidentally write things like<TT>if(a = b)</TT> ?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Yes, but it would also bejust alittle bitmore cumbersometo type all of the assignment statementswhich a typical program contains.</p><p>In any case,it's really too late to be worrying about this sort of thing now.The choices of <TT>=</TT> for assignment and <TT>==</TT> for comparisonwere made,rightly or wrongly,over two decades ago,and are not likely to be changed.(With respect to the question,many compilers and versions of <TT>lint</TT>will warn about <TT>if(a = b)</TT>and similar expressions;see also question <a href="faqcataae2.html?sec=style#revtest">17.4</a>.)</p><p>As a point of historical interest,the choices were made based on the observation that assignmentis more frequent than comparison,and so deserves fewer keystrokes.In fact,using <TT>=</TT> for assignment inC and its predecessor Brepresented a change fromB's own predecessorBCPL,which did use <TT>:=</TT>as its assignment operator.(See also question <a href="faqcat38c2.html?sec=misc#nameofc">20.38</a>).<hr><hr><hr><a name="with"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/with.html"><!-- qtag -->Question 20.23</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Does C have an equivalent to Pascal's<TT>with</TT> statement?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>No.The wayin Cto get quick and easy access to the fields of a structureis to declare a little local structure pointer variable(which,it must be admitted,is not quite as notationally convenientas a <TT>with</TT> statementand doesn't save quite as manykeystrokes,though it is probably safer).That is,if you have something unwieldy like<pre> structarray[complex_expression].a = structarray[complex_expression].b + structarray[complex_expression].c;</pre>you can replace it with<pre> struct whatever *p = &structarray[complex_expression]; p->a = p->b + p->c;</pre><hr><hr><hr><a name="nestfcns"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/nestfcns.html"><!-- qtag -->Question 20.24</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why doesn't C have nested functions?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's not trivial to implement nested functionssuch that they have the proper access to local variablesin the containing function(s),sotheywere deliberately left out of C as a simplification.(<TT>gcc</TT> does allow them, as an extension.)For many potential uses of nested functions(e.g. <TT>qsort</TT>comparisonfunctions),an adequate if slightlycumbersomesolutionis to use an adjacentfunction with <TT>static</TT> declaration,communicating if necessaryvia a few <TT>static</TT>variables.(Acleaner solution,though unsupported by <TT>qsort</TT>,is to pass arounda pointer toa structure containing the necessary context.)<hr><hr><hr><a name="assert"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/assert.html"><!-- qtag -->Question 20.24b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What is <TT>assert()</TT>and when would I use it?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It is a macro,defined in <TT><assert.h></TT>,for testing ``assertions''.An assertion essentially documents an assumptionbeing made by the programmer,an assumption which, if violated,would indicate a serious programming error.For example, a function which was supposed to be calledwith a non-null pointer could write<pre> assert(p != NULL);</pre>A failed assertion terminates the program.Assertions should <em>not</em> be used to catch expected errors,such as <TT>malloc</TT> or <TT>fopen</TT> failures.</p><p>References:K&R2 Sec. B6 pp. 253-4<br>ISO Sec. 7.2<br>H&S Sec. 19.1 p. 406<hr><hr><hr><a name="mixlang"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/mixlang.html"><!-- qtag -->Question 20.25</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I call FORTRAN(C++, BASIC, Pascal,Ada, LISP) functions from C?(And vice versa?)</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The answer is entirely dependenton the machine and the specificcalling sequences of the various compilers inuse, and may not be possible at all.Read your compiler documentation very carefully;sometimes there is a``mixed-language programming guide,''although the techniques for passing argumentsand ensuring correct run-time startupare often arcane.Besides arranging calling sequences correctly,you may also have to conspire between the various languagesto get aggregate data structures declared compatibly.</p><p>For FORTRAN,more information may be found inFORT.gzby Glenn Geers,available via anonymous ftp from suphys.physics.su.oz.au inthe src directory.Burkhard Burow's header file<TT>cfortran.h</TT>simplifies C/FORTRAN interfacing onmany popular machines.It is available via anonymous ftp from zebra.desy.deor at<a href="http://www-zeus.desy.de/~burow"><TT>http://www-zeus.desy.de/~burow</TT></a>.</p><p>In C++, a <TT>"C"</TT> modifierin an external function declarationindicates that the functionis to be calledusing C calling conventions.</p><p>In Ada, you can usethe<TT>Export</TT>and<TT>Convention</TT>pragmas,andtypes from the package <TT>Interfaces.C</TT>,to arrange for C-compatible calls, parameters, and data structures.</p><p>References:H&S Sec. 4.9.8 pp. 106-7<hr><hr><hr><a name="langtran"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/langtran.html"><!-- qtag -->Question 20.26</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Does anyone know of a programfor convertingPascal or FORTRAN(orLISP,Ada,awk,``Old'' C,...)to C?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Several freely distributable programs areavailable:<UL><li>p2cA Pascal to C converterwritten by Dave Gillespie,posted to comp.sources.unix inMarch, 1990 (Volume 21);also available by anonymous ftp from<a href="ftp://csvax.cs.caltech.edu/"><TT>csvax.cs.caltech.edu</TT></a>,file<a href="ftp://csvax.cs.caltech.edu/pub/p2c-1.20.tar.Z"><TT>pub/p2c-1.20.tar.Z</TT></a> .<li>ptocAnotherPascal to C converter,this one written in Pascal(comp.sources.unix, Volume 10, also patches in Volume 13?).<li>f2cA FORTRAN to C converterjointly developed by people from Bell Labs, Bellcore, and Carnegie Mellon.(f2c is really a FORTRAN compiler with a C backend--theoutput is not intended to be maintainable.)To find out more about f2c, send the mail message``send index from f2c''to netlib@research.att.com or research!netlib.(It is also available via anonymous ftp on<a href="ftp://netlib.att.com/"><TT>netlib.att.com</TT></a>,in directory<a href="ftp://netlib.att.com/netlib/f2c/"><TT>netlib/f2c/</TT></a>.)</UL><UL><li>A PL/M to C converter was posted to alt.sources in April, 1991.</UL></p><p>The following companies sell various translation tools and services:</p><UL><li><pre>Cobalt Blue2940 Union Ave., Suite CSan Jose, CA 95124 USA(+1) 408 723 0474</pre><li><pre>Promula Development Corp.3620 N. High St., Suite 301Columbus, OH 43214 USA(+1) 614 263 5454</pre><li><pre>Micro-Processor Services Inc.92 Stone Hurst LaneDix Hills, NY 11746 USA(+1) 516 499 4461</pre></UL><p>See also questions<a href="faqcat7d4b.html?sec=ansi#cproto">11.31</a>and<a href="faqcatccbd.html?sec=resources#sources">18.16</a>.<hr><hr><hr><a name="cplusplus"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../misc/cplusplus.html"><!-- qtag -->Question 20.27</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is C++ a superset of C?What are the differences between C and C++?Can I use a C++ compiler to compile C code?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>C++ was derived from C,and is largely based on it,but there are some legal C constructswhich are not legal C++.Conversely,ANSI C inherited several features from C++,including prototypes and <TT>const</TT>,so neither language is really a subset or superset of the other;the two also define the meaning of some common constructs differently.</p><p>The most important feature of C++ not found in C is of course the extended structureknown as a <TT>class</TT>whichalong with operator overloadingmakes object-oriented programming convenient.There areseveralother differences and new features:variables may be declared anywhere in a block;<TT>const</TT> variablesmay be true compile-timeconstants;structure tags are implicitly typedeffed;an <TT>&</TT> in a parameter declaration requests pass by reference;andthe <TT>new</TT> and <TT>delete</TT> operators,along with per-object constructors and destructors,simplify dynamic data structure management.There area host ofmechanisms tied up with classes and object-oriented programming:inheritance,<TT>friend</TT>s,virtual functions,templates,etc.(This list of C++ features is not intended to be complete;C++ programmers will notice many omissions.)</p><p>Somefeatures of C which keep it from being a strict subset of C++(that is, which keep C programs from necessarily being acceptable to C++ compilers)are that<TT>main</TT>may be called recursively,character constants are of type <TT>int</TT>,prototypes are not required,and<TT>void *</TT>implicitly converts to other pointer types.Also, every keyword in C++ which is not a keyword in Cis available in C as an identifier;C programs which use words like <TT>class</TT> and <TT>friend</TT>as ordinary identifiers will be rejected by C++ compilers.</p><p>In spite of the differences,manyC programs willcompile correctly in a C++ environment,and many recent compilersoffer both C and C++ compilation modes.(But it's usually a bad idea to compile straight C code as if it were C++;the languages are different enoughthat you'll generally get poor results.)</p><p>See alsoquestions <a href="faqcate107.html?sec=charstring#sizeofchar">8.9</a> and <a href="faqcat38c2.html?sec=misc#nestcomment">20.20</a>.</p><p>Additional links:<br><br><a href="http://www.research.att.com/~bs/bs_faq.html#C-is-subset">Bjarne Stroustrup's answer</a>on the subset/superset question<br><br>an<a href="../../misc/cplusplus.rs.html">article by Richard Stamp</a>listing some differences<br><br>an<a href="../../misc/cplusplus.nr.html">article by ``Noone Really
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -