📄 inttypes.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 decl.sgml dated Wed Dec 21 12:56:18 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/decl/inttypes.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:57:22 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 1.1</title><link href="exactsizes.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="exactsizes.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.1</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How should I decide which integer type to use?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>If you might need large values(above 32,767 or below -32,767),use <TT>long</TT>.Otherwise,ifspace is very important(i.e. ifthere are large arrays or many structures),use <TT>short</TT>.Otherwise,use <TT>int</TT>.If well-defined overflow characteristics are importantandnegative values are not,or if you want tosteer clear ofsign-extension problems when manipulating bits or bytes,useone ofthe corresponding <TT>unsigned</TT> types.(Bewarewhenmixing signed and unsignedvaluesin expressions,though;see question <a href="../expr/preservingrules.html">3.19</a>.)</p><p>Althoughcharacter types(especially <TT>unsigned char</TT>)canbe used as``tiny''integers,doing so issometimesmore trouble than it's worth.The compiler will have to emit extra codeto convert between <TT>char</TT> and <TT>int</TT>(making the executable larger),andunexpected sign extension canbe troublesome.(Using <TT>unsigned char</TT> can help;see question <a href="../stdio/getcharc.html">12.1</a> for a related problem.)</p><p>A similar space/time tradeoff applieswhen deciding between <TT>float</TT>and <TT>double</TT>.(Many compilers still convert all <TT>float</TT> values to <TT>double</TT>during expression evaluation.)None of the above rulesapplyifpointers to the variablemust have a particular type.</p><p>Variables referring to certain kinds of data,such as sizes of objects in memory,can and should used predefined abstract typessuch as <TT>size_t</TT>.</p><p>It's oftenincorrectlyassumed that C's types are defined to have certain, exact sizes.In fact,what's guaranteed is that:<UL><li>type <TT>char</TT> can hold values up to 127;<li>types <TT>short int</TT> and <TT>int</TT> can hold values up to 32,767;and<li>type <TT>long int</TT> can hold values up to 2,147,483,647.<li>something likethe relation<pre> sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)</pre>holds.<a href="fn1.html" rel=subdocument>[footnote]</a></UL></p><p>From these values, it can be inferred that <TT>char</TT> is at least 8 bits,<TT>short int</TT> and <TT>int</TT> are at least 16 bits,and <TT>long int</TT> is at least 32 bits.(The signed and unsigned versions of each typeare guaranteed to have the same size.)Under ANSI C,the maximum and minimum values for a particular machinecan be found in the header file <TT><limits.h></TT>;here is a summary:<table><tr><td>Base type</td><td>Minimum size (bits)</td><td>Minimum value (signed)</td><td>Maximum value (signed)</td><td>Maximum value (unsigned)</td></tr><tr><td><TT>char</TT></td><td>8</td><td>-127</td><td>127</td><td>255</td></tr><tr><td><TT>short</TT></td><td>16</td><td>-32,767</td><td>32,767</td><td>65,535</td></tr><tr><td><TT>int</TT></td><td>16</td><td>-32,767</td><td>32,767</td><td>65,535</td></tr><tr><td><TT>long</TT></td><td>32</td><td>-2,147,483,647</td><td>2,147,483,647</td><td>4,294,967,295</td></tr></table>(These values are the minimums guaranteed by the Standard.Many implementations allow larger values,but portable programs shouldn't depend on it.)</p><p>If for some reason you need to declare something with anexactsize(usually the only good reason for doing sois when attempting to conformto some externally-imposed storage layout,but see question<a href="../misc/binaryfiles.html">20.5</a>),be sure to encapsulate the choicebehind an appropriate typedef,but see question <a href="int16.html">1.3</a>.</p><p>If you need to manipulate huge values,larger than the guaranteed range of C's built-in types,you need an arbitrary-precision(or ``multiple precision'')arithmetic library;see question <a href="../resources/mplib.html">18.15d</a>.</p><p>References:K&R1 Sec. 2.2 p. 34<br>K&R2 Sec. 2.2 p. 36, Sec. A4.2 pp. 195-6, Sec. B11 p. 257<br>ISO Sec. 5.2.4.2.1, Sec. 6.1.2.5<br>H&S Secs. 5.1,5.2 pp. 110-114<br></p><!-- aend --><p><hr><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="exactsizes.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/inttypes.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:57:22 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -