⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faqcat7d4b.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file ansi.sgml dated Wed Aug 11 00:43:45 2004 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=ansi by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:03 GMT --><head><base ><meta name=GENERATOR content="faqproc"><title>ANSI/ISO Standard C</title></head><body bgcolor="#ffffff"><H1>11. ANSI/ISO Standard C</H1><a name="ansi1"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/ansi1.html"><!-- qtag -->Question 11.1</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What is the ``ANSI C Standard?''</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>In 1983,the American National Standards Institute(ANSI)commissioned acommittee, X3J11,to standardize the C language.After a long, arduousprocess,including several widespread public reviews,the committee'sworkwasfinallyratified asANSX3.159-1989on December 14, 1989,and publishedin the spring of 1990.For the most part, ANSI C standardized existing practice,with a few additions from C++(most notablyfunction prototypes) andsupport for multinational character sets (including thecontroversialtrigraph sequences).The ANSI C standard also formalized the C run-time librarysupport routines.</p><p>A year or so later,the Standard wasadopted asan international standard,ISO/IEC&nbsp;9899:1990,and this ISO Standard replaced the earlier X3.159even within the United States(where it was known asANSI/ISO 9899-1990 [1992]).As an ISO Standard,it issubject to ongoing revisionthrough the release of Technical Corrigenda and Normative Addenda.</p><p>In 1994,Technical Corrigendum 1(TC1)amended the Standardin about 40 places,most of them minor corrections or clarifications,andNormative Addendum 1(NA1)added about 50 pages of new material,mostly specifying new library functions for internationalization.In 1995, TC2 added a few more minor corrections.</p><p>Most recently,a major revision of the Standard,``C99'',has been completed and adopted.</p><p>Several versions of the Standard,including C99 andthe original ANSI Standard,haveincluded a ``Rationale,''explaining many of its decisions,and discussinga number of subtle points,including several of those covered here.<hr><hr><hr><a name="avail"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/avail.html"><!-- qtag -->Question 11.2</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I get a copy of the Standard?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>An electronic (PDF) copy is available on-line,for US$18,from <a href="http://www.ansi.org/"><TT>www.ansi.org</TT></a>.Paper copiesare availablein the United Statesfrom<blockquote><pre>American National Standards Institute11 W. 42nd St., 13th floorNew York, NY  10036  USA(+1) 212 642 4900</pre></blockquote>and<blockquote><pre>Global Engineering Documents15 Inverness Way EEnglewood, CO  80112  USA(+1) 303 397 2715(800) 854 7179  (U.S. &amp; Canada)</pre></blockquote>In other countries, contact the appropriate national standards body,or ISO in Geneva at:<blockquote><pre>ISO SalesCase Postale 56CH-1211 Geneve 20Switzerland</pre></blockquote><br><br>(orsee URL<a href="http://www.iso.ch/"><TT>http://www.iso.ch</TT></a>orcheckthe comp.std.internat FAQ list,Standards.Faq).</p><p>Themistitled<I>Annotated ANSI C Standard</I>,with annotations by Herbert Schildt,containsmost of thetextof ISO&nbsp;9899;it is published by Osborne/McGraw-Hill,ISBN 0-07-881952-0,and sells in the U.S. for approximately $40.It has been suggestedthat the price differential between this work andthe official standard reflects the value of the annotations:theyare plagued by numerouserrors and omissions,and a few pages of the Standard itself are missing.Many people on the net recommend ignoring the annotationsentirely.A review of the annotations(``annotated annotations'')by Clive Feather can be foundon the web at<a href="http://www.lysator.liu.se/c/schildt.html"><TT>http://www.lysator.liu.se/c/schildt.html</TT></a>.</p><p>The text of theoriginal ANSI Rationalecan be obtained byanonymous ftpfrom ftp.uu.net(see question<a href="faqcatccbd.html?sec=resources#sources">18.16</a>)in directorydoc/standards/ansi/X3.159-1989,and is also available on the web at<a href="http://www.lysator.liu.se/c/rat/title.html"><TT>http://www.lysator.liu.se/c/rat/title.html</TT></a>.That Rationalehasalsobeen printed by Silicon Press,ISBN 0-929306-07-4.</p><p>Public review drafts of C9X were available from<a href="http://www.dkuug.dk/JTC1/SC22/WG14/"><TT>ISO/IEC JTC1/SC22/WG14'sweb site</TT></a>.</p><p>See also question <a href="faqcat7d4b.html?sec=ansi#updates">11.2b</a>.<hr><hr><hr><a name="updates"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/updates.html"><!-- qtag -->Question 11.2b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Where can I get information about updates to the Standard?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>You can find information(including C9X drafts)atthe web sites<a href="http://www.lysator.liu.se/c/index.html"><TT>http://www.lysator.liu.se/c/index.html</TT></a>,<a href="http://www.dkuug.dk/JTC1/SC22/WG14/"><TT>http://www.dkuug.dk/JTC1/SC22/WG14/</TT></a>,and<a href="http://www.dmk.com/"><TT>http://www.dmk.com/</TT></a>.<hr><hr><hr><a name="argpromos"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/argpromos.html"><!-- qtag -->Question 11.3</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>My ANSI compilercomplains about a mismatch when it sees<pre>	extern int func(float);	int func(x)	float x;	{ ...</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>You have mixed the new-styleprototypedeclaration ``<TT>extern&nbsp;int&nbsp;func(float);</TT>''with the old-style definition ``<TT>int&nbsp;func(x) float&nbsp;x;</TT>''.It is usuallypossibleto mix thetwostyles(see question<a href="faqcat7d4b.html?sec=ansi#mixoldandnew">11.4</a>),but not in this case.</p><p>Old C(and ANSI C, in the absence of prototypes,and invariable-length argument lists;see question <a href="faqcat744e.html?sec=varargs#promos">15.2</a>)``widens'' certain arguments when they are passed to functions.<TT>float</TT>sarepromoted to <TT>double</TT>,andcharacters andshort integersare promoted to <TT>int</TT>.(For old-style function definitions,the values are automatically converted backto the corresponding narrower typeswithin the body of the called function,if they are declared that way there.)Therefore,the old-style definition aboveactually says that <TT>func</TT> takes a <TT>double</TT>(which will be converted to <TT>float</TT> inside the function).</p><p>This problem can be fixed either by using new-style syntaxconsistently in the definition:<pre>	int func(float x) { ... }</pre></p><p>or by changing the new-style prototype declaration to match the old-styledefinition:<pre>	extern int func(double);</pre>(In this case, it would be clearest to change the old-styledefinition to use <TT>double</TT> as well.<a href="../../ansi/fn49.html" rel=subdocument>[footnote]</a>)</p><p>Itis arguably muchsaferto avoid ``narrow''(<TT>char</TT>, <TT>short&nbsp;int</TT>, and <TT>float</TT>)function arguments and returntypesaltogether.</p><p>See also question<a href="faqcatd3c2.html?sec=decl#implfdecl">1.25</a>.</p><p>References:K&amp;R1 Sec. A7.1 p. 186<br>K&amp;R2 Sec. A7.3.2 p. 202<br>ISO Sec. 6.3.2.2, Sec. 6.5.4.3<br>Rationale Sec. 3.3.2.2, Sec. 3.5.4.3<br>H&amp;S Sec. 9.2 pp. 265-7, Sec. 9.4 pp. 272-3<hr><hr><hr><a name="mixoldandnew"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/mixoldandnew.html"><!-- qtag -->Question 11.4</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Can you mix old-style and new-stylefunction syntax?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Doing so islegal(and can be useful for backwards compatibility),but requires a certain amount of care(seeespeciallyquestion<a href="faqcat7d4b.html?sec=ansi#argpromos">11.3</a>).Modern practice,however,is touse the prototyped form in both declarations and definitions.(Theold-style syntax is marked as obsolescent,so official support for it may be removed some day.)</p><p>References:ISO Sec. 6.7.1, Sec. 6.9.5<br>H&amp;S Sec. 9.2.2 pp. 265-7, Sec. 9.2.5 pp. 269-70<hr><hr><hr><a name="structinproto"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/structinproto.html"><!-- qtag -->Question 11.5</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why does the declaration<pre>extern int f(struct x *p);</pre>give mean obscurewarning messageabout``struct x declared inside parameter list''?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>In a quirk of C's normal block scoping rules,astructure declared(or even mentioned)for the first timewithin a prototypecannot be compatible with other structuresdeclared in the same source file.(The problem is that the structure and the tag goout of scope at the end of the prototype;see question <a href="faqcatd3c2.html?sec=decl#namespace">1.29</a>.)</p><p>To resolve the problem,you should probably rearrange things so thatthe actual declaration of the structureprecedes the function prototype(s) using it.(Usually,boththe prototypeandthe structure declarationwill end up in the same header file,so that the one can reference the other.)If you must mention a hitherto-unseen structurein a prototype,precede the prototype with thevacuous-looking declaration<pre>	struct x;</pre>whichplaces an(incomplete)declaration of <TT>struct&nbsp;x</TT> at file scope,so that all following declarations involving <TT>struct&nbsp;x</TT>can at least be sure they're referring to the same <TT>struct&nbsp;x</TT>.</p><p>References:ISO Sec. 6.1.2.1, Sec. 6.1.2.6, Sec. 6.5.2.3<hr><hr><hr><a name="varargproto"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/varargproto.html"><!-- qtag -->Question 11.6</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I had a frustrating problem which turned out to be caused by the line<pre>	printf("%d", n);</pre>where <TT>n</TT> was actually a <TT>long&nbsp;int</TT>.I thought that ANSI function prototypeswere supposed to guard against argument type mismatches like this.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat744e.html?sec=varargs#proto2">15.3</a>.<hr><hr><hr><a name="varargproto2"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/varargproto2.html"><!-- qtag -->Question 11.7</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I heard that you have to <TT>#include</TT> <TT>&lt;stdio.h&gt;</TT>before calling <TT>printf</TT>.Why?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat744e.html?sec=varargs#protos">15.1</a>.<hr><hr><hr><a name="constasconst"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/constasconst.html"><!-- qtag -->Question 11.8</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I don't understand why I can'tuse <TT>const</TT> values in initializers and arraydimensions,as in<pre>	const int n = 5;	int a[n];</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The <TT>const</TT> qualifier really means ``read-only'';an object so qualified is arun-time objectwhichcannot (normally) be assigned to.The value of a <TT>const</TT>-qualified object istherefore<em>not</em>a constant expressionin the full sense of the term,and cannot be used for array dimensions, case labels, and the like.(C is unlike C++ in this regard.)When you need a true compile-time constant,use a preprocessor <TT>#define</TT>(or perhaps an <TT>enum</TT>).</p><p>References:ISO Sec. 6.4<br>H&amp;S Secs. 7.11.2,7.11.3 pp. 226-7<hr><hr><hr><a name="strlitnotconst"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../ansi/strlitnotconst.html"><!-- qtag -->Question 11.8b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>If you can't modify string literals,why aren't they defined as beingarrays of <TT>const</TT> characters?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>One reason is that so very much code contains lines like<pre>	char *p = "Hello, world!";</pre>which are not necessarily incorrect.These lines wouldsufferthe diagnostic messages,but it's really any later attempt to modify what <TT>p</TT> points towhich would be problems.<br></p><p>See also question <a href="faqcatd3c2.html?sec=decl#strlitinit">1.32</a>.<hr><hr><hr><a name="constptrconst"><h1>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -