📄 initval.html
字号:
<!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:45 2005 by faqproc version 2.7 --><!-- from source file init.sgml dated Fri Jul 2 21:52:44 2004 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/decl/initval.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:45 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 1.30</title><link href="namespace.html" rev=precedes><link href="autoaggrinit.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="namespace.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="autoaggrinit.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 1.30</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Whatam I allowed toassume about the initial values of variablesand arrayswhich are not explicitly initialized?<br>If global variables start out as ``zero'',is that good enoughfor null pointers and floating-point zeroes?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Uninitializedvariableswith<a href="../sx1/index.html#static"><dfn>static</dfn></a>duration(that is,those declared outside of functions,and those declared with the storage class <TT>static</TT>),are guaranteedtostart out aszero,justas if the programmer had typed ``<TT>= 0</TT>''or ``<TT>= {0}</TT>''.Therefore,such variables areimplicitlyinitializedto the null pointer(of the correct type;see also section<a href="../null/index.html">5</a>)if they are pointers,andto 0.0 if they are floating-point.<a href="zerofill.html" rel=subdocument>[footnote]</a></p><p>Variables with<a href="../sx1/index.html#automatic"><dfn>automatic</dfn></a>duration(i.e. local variables without the <TT>static</TT> storage class)start out containing garbage,unless they are explicitly initialized.(Nothing useful can be predicted about the garbage.)If theydo have initializers,they are initialized each time the functioniscalled(or,for variables local to inner blocks,each time the block is enteredat thetop<a href="jumpinto.html" rel=subdocument>[footnote]</a>).</p><p>These rules do apply to arrays and structures(termed <a href="../sx1/index.html#aggregates"><dfn>aggregates</dfn></a>);arrays and structures are considered ``variables''as far as initialization is concerned.Whenan automatic array or structurehasa partial initializer,the remainderis initialized to 0,just as for statics.<a href="krerr.html" rel=subdocument>[footnote]</a>See also question <a href="autoaggrinit.html">1.31</a>.</p><p>Finally,dynamically-allocated memoryobtained with <TT>malloc</TT> and <TT>realloc</TT>is likely to contain garbage,and must be initialized by the calling program,as appropriate.Memory obtained with <TT>calloc</TT>isall-bits-0,but this is not necessarily usefulfor pointer or floating-point values(see question<a href="../malloc/calloc.html">7.31</a>,and section<a href="../null/index.html">5</a>).</p><p>References:K&R1 Sec. 4.9 pp. 82-4<br>K&R2 Sec. 4.9 pp. 85-86<br>ISO Sec. 6.5.7, Sec. 7.10.3.1, Sec. 7.10.5.3<br>H&S Sec. 4.2.8 pp. 72-3, Sec. 4.6 pp. 92-3, Sec. 4.6.2 pp. 94-5, Sec. 4.6.3 p. 96, Sec. 16.1 p. 386<br></p><!-- aend --><p><hr><a href="namespace.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="autoaggrinit.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/decl/initval.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:45 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -