lang.texi 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. @c This node must have no pointers.
  2. @node Language Features
  3. @c @node Language Features, Library Summary, , Top
  4. @c %MENU% C language features provided by the library
  5. @appendix C Language Facilities in the Library
  6. Some of the facilities implemented by the C library really should be
  7. thought of as parts of the C language itself. These facilities ought to
  8. be documented in the C Language Manual, not in the library manual; but
  9. since we don't have the language manual yet, and documentation for these
  10. features has been written, we are publishing it here.
  11. @menu
  12. * Consistency Checking:: Using @code{assert} to abort if
  13. something ``impossible'' happens.
  14. * Variadic Functions:: Defining functions with varying numbers
  15. of args.
  16. * Null Pointer Constant:: The macro @code{NULL}.
  17. * Important Data Types:: Data types for object sizes.
  18. * Data Type Measurements:: Parameters of data type representations.
  19. @end menu
  20. @node Consistency Checking
  21. @section Explicitly Checking Internal Consistency
  22. @cindex consistency checking
  23. @cindex impossible events
  24. @cindex assertions
  25. When you're writing a program, it's often a good idea to put in checks
  26. at strategic places for ``impossible'' errors or violations of basic
  27. assumptions. These kinds of checks are helpful in debugging problems
  28. with the interfaces between different parts of the program, for example.
  29. @pindex assert.h
  30. The @code{assert} macro, defined in the header file @file{assert.h},
  31. provides a convenient way to abort the program while printing a message
  32. about where in the program the error was detected.
  33. @vindex NDEBUG
  34. Once you think your program is debugged, you can disable the error
  35. checks performed by the @code{assert} macro by recompiling with the
  36. macro @code{NDEBUG} defined. This means you don't actually have to
  37. change the program source code to disable these checks.
  38. But disabling these consistency checks is undesirable unless they make
  39. the program significantly slower. All else being equal, more error
  40. checking is good no matter who is running the program. A wise user
  41. would rather have a program crash, visibly, than have it return nonsense
  42. without indicating anything might be wrong.
  43. @deftypefn Macro void assert (int @var{expression})
  44. @standards{ISO, assert.h}
  45. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  46. @c assert_fail_base calls asprintf, and fflushes stderr.
  47. Verify the programmer's belief that @var{expression} is nonzero at
  48. this point in the program.
  49. If @code{NDEBUG} is not defined, @code{assert} tests the value of
  50. @var{expression}. If it is false (zero), @code{assert} aborts the
  51. program (@pxref{Aborting a Program}) after printing a message of the
  52. form:
  53. @smallexample
  54. @file{@var{file}}:@var{linenum}: @var{function}: Assertion `@var{expression}' failed.
  55. @end smallexample
  56. @noindent
  57. on the standard error stream @code{stderr} (@pxref{Standard Streams}).
  58. The filename and line number are taken from the C preprocessor macros
  59. @code{__FILE__} and @code{__LINE__} and specify where the call to
  60. @code{assert} was made. When using the GNU C compiler, the name of
  61. the function which calls @code{assert} is taken from the built-in
  62. variable @code{__PRETTY_FUNCTION__}; with older compilers, the function
  63. name and following colon are omitted.
  64. If the preprocessor macro @code{NDEBUG} is defined before
  65. @file{assert.h} is included, the @code{assert} macro is defined to do
  66. absolutely nothing.
  67. @strong{Warning:} Even the argument expression @var{expression} is not
  68. evaluated if @code{NDEBUG} is in effect. So never use @code{assert}
  69. with arguments that involve side effects. For example, @code{assert
  70. (++i > 0);} is a bad idea, because @code{i} will not be incremented if
  71. @code{NDEBUG} is defined.
  72. @end deftypefn
  73. Sometimes the ``impossible'' condition you want to check for is an error
  74. return from an operating system function. Then it is useful to display
  75. not only where the program crashes, but also what error was returned.
  76. The @code{assert_perror} macro makes this easy.
  77. @deftypefn Macro void assert_perror (int @var{errnum})
  78. @standards{GNU, assert.h}
  79. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  80. @c assert_fail_base calls asprintf, and fflushes stderr.
  81. Similar to @code{assert}, but verifies that @var{errnum} is zero.
  82. If @code{NDEBUG} is not defined, @code{assert_perror} tests the value of
  83. @var{errnum}. If it is nonzero, @code{assert_perror} aborts the program
  84. after printing a message of the form:
  85. @smallexample
  86. @file{@var{file}}:@var{linenum}: @var{function}: @var{error text}
  87. @end smallexample
  88. @noindent
  89. on the standard error stream. The file name, line number, and function
  90. name are as for @code{assert}. The error text is the result of
  91. @w{@code{strerror (@var{errnum})}}. @xref{Error Messages}.
  92. Like @code{assert}, if @code{NDEBUG} is defined before @file{assert.h}
  93. is included, the @code{assert_perror} macro does absolutely nothing. It
  94. does not evaluate the argument, so @var{errnum} should not have any side
  95. effects. It is best for @var{errnum} to be just a simple variable
  96. reference; often it will be @code{errno}.
  97. This macro is a GNU extension.
  98. @end deftypefn
  99. @strong{Usage note:} The @code{assert} facility is designed for
  100. detecting @emph{internal inconsistency}; it is not suitable for
  101. reporting invalid input or improper usage by the @emph{user} of the
  102. program.
  103. The information in the diagnostic messages printed by the @code{assert}
  104. and @code{assert_perror} macro is intended to help you, the programmer,
  105. track down the cause of a bug, but is not really useful for telling a user
  106. of your program why his or her input was invalid or why a command could not
  107. be carried out. What's more, your program should not abort when given
  108. invalid input, as @code{assert} would do---it should exit with nonzero
  109. status (@pxref{Exit Status}) after printing its error messages, or perhaps
  110. read another command or move on to the next input file.
  111. @xref{Error Messages}, for information on printing error messages for
  112. problems that @emph{do not} represent bugs in the program.
  113. @node Variadic Functions
  114. @section Variadic Functions
  115. @cindex variable number of arguments
  116. @cindex variadic functions
  117. @cindex optional arguments
  118. @w{ISO C} defines a syntax for declaring a function to take a variable
  119. number or type of arguments. (Such functions are referred to as
  120. @dfn{varargs functions} or @dfn{variadic functions}.) However, the
  121. language itself provides no mechanism for such functions to access their
  122. non-required arguments; instead, you use the variable arguments macros
  123. defined in @file{stdarg.h}.
  124. This section describes how to declare variadic functions, how to write
  125. them, and how to call them properly.
  126. @strong{Compatibility Note:} Many older C dialects provide a similar,
  127. but incompatible, mechanism for defining functions with variable numbers
  128. of arguments, using @file{varargs.h}.
  129. @menu
  130. * Why Variadic:: Reasons for making functions take
  131. variable arguments.
  132. * How Variadic:: How to define and call variadic functions.
  133. * Variadic Example:: A complete example.
  134. @end menu
  135. @node Why Variadic
  136. @subsection Why Variadic Functions are Used
  137. Ordinary C functions take a fixed number of arguments. When you define
  138. a function, you specify the data type for each argument. Every call to
  139. the function should supply the expected number of arguments, with types
  140. that can be converted to the specified ones. Thus, if the function
  141. @samp{foo} is declared with @code{int foo (int, char *);} then you must
  142. call it with two arguments, a number (any kind will do) and a string
  143. pointer.
  144. But some functions perform operations that can meaningfully accept an
  145. unlimited number of arguments.
  146. In some cases a function can handle any number of values by operating on
  147. all of them as a block. For example, consider a function that allocates
  148. a one-dimensional array with @code{malloc} to hold a specified set of
  149. values. This operation makes sense for any number of values, as long as
  150. the length of the array corresponds to that number. Without facilities
  151. for variable arguments, you would have to define a separate function for
  152. each possible array size.
  153. The library function @code{printf} (@pxref{Formatted Output}) is an
  154. example of another class of function where variable arguments are
  155. useful. This function prints its arguments (which can vary in type as
  156. well as number) under the control of a format template string.
  157. These are good reasons to define a @dfn{variadic} function which can
  158. handle as many arguments as the caller chooses to pass.
  159. Some functions such as @code{open} take a fixed set of arguments, but
  160. occasionally ignore the last few. Strict adherence to @w{ISO C} requires
  161. these functions to be defined as variadic; in practice, however, the GNU
  162. C compiler and most other C compilers let you define such a function to
  163. take a fixed set of arguments---the most it can ever use---and then only
  164. @emph{declare} the function as variadic (or not declare its arguments
  165. at all!).
  166. @node How Variadic
  167. @subsection How Variadic Functions are Defined and Used
  168. Defining and using a variadic function involves three steps:
  169. @itemize @bullet
  170. @item
  171. @emph{Define} the function as variadic, using an ellipsis
  172. (@samp{@dots{}}) in the argument list, and using special macros to
  173. access the variable arguments. @xref{Receiving Arguments}.
  174. @item
  175. @emph{Declare} the function as variadic, using a prototype with an
  176. ellipsis (@samp{@dots{}}), in all the files which call it.
  177. @xref{Variadic Prototypes}.
  178. @item
  179. @emph{Call} the function by writing the fixed arguments followed by the
  180. additional variable arguments. @xref{Calling Variadics}.
  181. @end itemize
  182. @menu
  183. * Variadic Prototypes:: How to make a prototype for a function
  184. with variable arguments.
  185. * Receiving Arguments:: Steps you must follow to access the
  186. optional argument values.
  187. * How Many Arguments:: How to decide whether there are more arguments.
  188. * Calling Variadics:: Things you need to know about calling
  189. variable arguments functions.
  190. * Argument Macros:: Detailed specification of the macros
  191. for accessing variable arguments.
  192. @end menu
  193. @node Variadic Prototypes
  194. @subsubsection Syntax for Variable Arguments
  195. @cindex function prototypes (variadic)
  196. @cindex prototypes for variadic functions
  197. @cindex variadic function prototypes
  198. A function that accepts a variable number of arguments must be declared
  199. with a prototype that says so. You write the fixed arguments as usual,
  200. and then tack on @samp{@dots{}} to indicate the possibility of
  201. additional arguments. The syntax of @w{ISO C} requires at least one fixed
  202. argument before the @samp{@dots{}}. For example,
  203. @smallexample
  204. int
  205. func (const char *a, int b, @dots{})
  206. @{
  207. @dots{}
  208. @}
  209. @end smallexample
  210. @noindent
  211. defines a function @code{func} which returns an @code{int} and takes two
  212. required arguments, a @code{const char *} and an @code{int}. These are
  213. followed by any number of anonymous arguments.
  214. @strong{Portability note:} For some C compilers, the last required
  215. argument must not be declared @code{register} in the function
  216. definition. Furthermore, this argument's type must be
  217. @dfn{self-promoting}: that is, the default promotions must not change
  218. its type. This rules out array and function types, as well as
  219. @code{float}, @code{char} (whether signed or not) and @w{@code{short int}}
  220. (whether signed or not). This is actually an @w{ISO C} requirement.
  221. @node Receiving Arguments
  222. @subsubsection Receiving the Argument Values
  223. @cindex variadic function argument access
  224. @cindex arguments (variadic functions)
  225. Ordinary fixed arguments have individual names, and you can use these
  226. names to access their values. But optional arguments have no
  227. names---nothing but @samp{@dots{}}. How can you access them?
  228. @pindex stdarg.h
  229. The only way to access them is sequentially, in the order they were
  230. written, and you must use special macros from @file{stdarg.h} in the
  231. following three step process:
  232. @enumerate
  233. @item
  234. You initialize an argument pointer variable of type @code{va_list} using
  235. @code{va_start}. The argument pointer when initialized points to the
  236. first optional argument.
  237. @item
  238. You access the optional arguments by successive calls to @code{va_arg}.
  239. The first call to @code{va_arg} gives you the first optional argument,
  240. the next call gives you the second, and so on.
  241. You can stop at any time if you wish to ignore any remaining optional
  242. arguments. It is perfectly all right for a function to access fewer
  243. arguments than were supplied in the call, but you will get garbage
  244. values if you try to access too many arguments.
  245. @item
  246. You indicate that you are finished with the argument pointer variable by
  247. calling @code{va_end}.
  248. (In practice, with most C compilers, calling @code{va_end} does nothing.
  249. This is always true in the GNU C compiler. But you might as well call
  250. @code{va_end} just in case your program is someday compiled with a peculiar
  251. compiler.)
  252. @end enumerate
  253. @xref{Argument Macros}, for the full definitions of @code{va_start},
  254. @code{va_arg} and @code{va_end}.
  255. Steps 1 and 3 must be performed in the function that accepts the
  256. optional arguments. However, you can pass the @code{va_list} variable
  257. as an argument to another function and perform all or part of step 2
  258. there.
  259. You can perform the entire sequence of three steps multiple times
  260. within a single function invocation. If you want to ignore the optional
  261. arguments, you can do these steps zero times.
  262. You can have more than one argument pointer variable if you like. You
  263. can initialize each variable with @code{va_start} when you wish, and
  264. then you can fetch arguments with each argument pointer as you wish.
  265. Each argument pointer variable will sequence through the same set of
  266. argument values, but at its own pace.
  267. @strong{Portability note:} With some compilers, once you pass an
  268. argument pointer value to a subroutine, you must not keep using the same
  269. argument pointer value after that subroutine returns. For full
  270. portability, you should just pass it to @code{va_end}. This is actually
  271. an @w{ISO C} requirement, but most ANSI C compilers work happily
  272. regardless.
  273. @node How Many Arguments
  274. @subsubsection How Many Arguments Were Supplied
  275. @cindex number of arguments passed
  276. @cindex how many arguments
  277. @cindex arguments, how many
  278. There is no general way for a function to determine the number and type
  279. of the optional arguments it was called with. So whoever designs the
  280. function typically designs a convention for the caller to specify the number
  281. and type of arguments. It is up to you to define an appropriate calling
  282. convention for each variadic function, and write all calls accordingly.
  283. One kind of calling convention is to pass the number of optional
  284. arguments as one of the fixed arguments. This convention works provided
  285. all of the optional arguments are of the same type.
  286. A similar alternative is to have one of the required arguments be a bit
  287. mask, with a bit for each possible purpose for which an optional
  288. argument might be supplied. You would test the bits in a predefined
  289. sequence; if the bit is set, fetch the value of the next argument,
  290. otherwise use a default value.
  291. A required argument can be used as a pattern to specify both the number
  292. and types of the optional arguments. The format string argument to
  293. @code{printf} is one example of this (@pxref{Formatted Output Functions}).
  294. Another possibility is to pass an ``end marker'' value as the last
  295. optional argument. For example, for a function that manipulates an
  296. arbitrary number of pointer arguments, a null pointer might indicate the
  297. end of the argument list. (This assumes that a null pointer isn't
  298. otherwise meaningful to the function.) The @code{execl} function works
  299. in just this way; see @ref{Executing a File}.
  300. @node Calling Variadics
  301. @subsubsection Calling Variadic Functions
  302. @cindex variadic functions, calling
  303. @cindex calling variadic functions
  304. @cindex declaring variadic functions
  305. You don't have to do anything special to call a variadic function.
  306. Just put the arguments (required arguments, followed by optional ones)
  307. inside parentheses, separated by commas, as usual. But you must declare
  308. the function with a prototype and know how the argument values are converted.
  309. In principle, functions that are @emph{defined} to be variadic must also
  310. be @emph{declared} to be variadic using a function prototype whenever
  311. you call them. (@xref{Variadic Prototypes}, for how.) This is because
  312. some C compilers use a different calling convention to pass the same set
  313. of argument values to a function depending on whether that function
  314. takes variable arguments or fixed arguments.
  315. In practice, the GNU C compiler always passes a given set of argument
  316. types in the same way regardless of whether they are optional or
  317. required. So, as long as the argument types are self-promoting, you can
  318. safely omit declaring them. Usually it is a good idea to declare the
  319. argument types for variadic functions, and indeed for all functions.
  320. But there are a few functions which it is extremely convenient not to
  321. have to declare as variadic---for example, @code{open} and
  322. @code{printf}.
  323. @cindex default argument promotions
  324. @cindex argument promotion
  325. Since the prototype doesn't specify types for optional arguments, in a
  326. call to a variadic function the @dfn{default argument promotions} are
  327. performed on the optional argument values. This means the objects of
  328. type @code{char} or @w{@code{short int}} (whether signed or not) are
  329. promoted to either @code{int} or @w{@code{unsigned int}}, as
  330. appropriate; and that objects of type @code{float} are promoted to type
  331. @code{double}. So, if the caller passes a @code{char} as an optional
  332. argument, it is promoted to an @code{int}, and the function can access
  333. it with @code{va_arg (@var{ap}, int)}.
  334. Conversion of the required arguments is controlled by the function
  335. prototype in the usual way: the argument expression is converted to the
  336. declared argument type as if it were being assigned to a variable of
  337. that type.
  338. @node Argument Macros
  339. @subsubsection Argument Access Macros
  340. Here are descriptions of the macros used to retrieve variable arguments.
  341. These macros are defined in the header file @file{stdarg.h}.
  342. @pindex stdarg.h
  343. @deftp {Data Type} va_list
  344. @standards{ISO, stdarg.h}
  345. The type @code{va_list} is used for argument pointer variables.
  346. @end deftp
  347. @deftypefn {Macro} void va_start (va_list @var{ap}, @var{last-required})
  348. @standards{ISO, stdarg.h}
  349. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  350. @c This is no longer provided by glibc, but rather by the compiler.
  351. This macro initializes the argument pointer variable @var{ap} to point
  352. to the first of the optional arguments of the current function;
  353. @var{last-required} must be the last required argument to the function.
  354. @end deftypefn
  355. @deftypefn {Macro} @var{type} va_arg (va_list @var{ap}, @var{type})
  356. @standards{ISO, stdarg.h}
  357. @safety{@prelim{}@mtsafe{@mtsrace{:ap}}@assafe{}@acunsafe{@acucorrupt{}}}
  358. @c This is no longer provided by glibc, but rather by the compiler.
  359. @c Unlike the other va_ macros, that either start/end the lifetime of
  360. @c the va_list object or don't modify it, this one modifies ap, and it
  361. @c may leave it in a partially updated state.
  362. The @code{va_arg} macro returns the value of the next optional argument,
  363. and modifies the value of @var{ap} to point to the subsequent argument.
  364. Thus, successive uses of @code{va_arg} return successive optional
  365. arguments.
  366. The type of the value returned by @code{va_arg} is @var{type} as
  367. specified in the call. @var{type} must be a self-promoting type (not
  368. @code{char} or @code{short int} or @code{float}) that matches the type
  369. of the actual argument.
  370. @end deftypefn
  371. @deftypefn {Macro} void va_end (va_list @var{ap})
  372. @standards{ISO, stdarg.h}
  373. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  374. @c This is no longer provided by glibc, but rather by the compiler.
  375. This ends the use of @var{ap}. After a @code{va_end} call, further
  376. @code{va_arg} calls with the same @var{ap} may not work. You should invoke
  377. @code{va_end} before returning from the function in which @code{va_start}
  378. was invoked with the same @var{ap} argument.
  379. In @theglibc{}, @code{va_end} does nothing, and you need not ever
  380. use it except for reasons of portability.
  381. @refill
  382. @end deftypefn
  383. Sometimes it is necessary to parse the list of parameters more than once
  384. or one wants to remember a certain position in the parameter list. To
  385. do this, one will have to make a copy of the current value of the
  386. argument. But @code{va_list} is an opaque type and one cannot necessarily
  387. assign the value of one variable of type @code{va_list} to another variable
  388. of the same type.
  389. @deftypefn {Macro} void va_copy (va_list @var{dest}, va_list @var{src})
  390. @deftypefnx {Macro} void __va_copy (va_list @var{dest}, va_list @var{src})
  391. @standardsx{va_copy, C99, stdarg.h}
  392. @standardsx{__va_copy, GNU, stdarg.h}
  393. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  394. The @code{va_copy} macro allows copying of objects of type
  395. @code{va_list} even if this is not an integral type. The argument pointer
  396. in @var{dest} is initialized to point to the same argument as the
  397. pointer in @var{src}.
  398. @code{va_copy} was added in ISO C99. When building for strict
  399. conformance to ISO C90 (@samp{gcc -std=c90}), it is not available.
  400. GCC provides @code{__va_copy}, as an extension, in any standards mode;
  401. before GCC 3.0, it was the only macro for this functionality.
  402. These macros are no longer provided by @theglibc{}, but rather by the
  403. compiler.
  404. @end deftypefn
  405. If you want to use @code{va_copy} and be portable to pre-C99 systems,
  406. you should always be prepared for the
  407. possibility that this macro will not be available. On architectures where a
  408. simple assignment is invalid, hopefully @code{va_copy} @emph{will} be available,
  409. so one should always write something like this if concerned about
  410. pre-C99 portability:
  411. @smallexample
  412. @{
  413. va_list ap, save;
  414. @dots{}
  415. #ifdef va_copy
  416. va_copy (save, ap);
  417. #else
  418. save = ap;
  419. #endif
  420. @dots{}
  421. @}
  422. @end smallexample
  423. @node Variadic Example
  424. @subsection Example of a Variadic Function
  425. Here is a complete sample function that accepts a variable number of
  426. arguments. The first argument to the function is the count of remaining
  427. arguments, which are added up and the result returned. While trivial,
  428. this function is sufficient to illustrate how to use the variable
  429. arguments facility.
  430. @comment Yes, this example has been tested.
  431. @smallexample
  432. @include add.c.texi
  433. @end smallexample
  434. @node Null Pointer Constant
  435. @section Null Pointer Constant
  436. @cindex null pointer constant
  437. The null pointer constant is guaranteed not to point to any real object.
  438. You can assign it to any pointer variable since it has type @code{void
  439. *}. The preferred way to write a null pointer constant is with
  440. @code{NULL}.
  441. @deftypevr Macro {void *} NULL
  442. @standards{ISO, stddef.h}
  443. This is a null pointer constant.
  444. @end deftypevr
  445. You can also use @code{0} or @code{(void *)0} as a null pointer
  446. constant, but using @code{NULL} is cleaner because it makes the purpose
  447. of the constant more evident.
  448. If you use the null pointer constant as a function argument, then for
  449. complete portability you should make sure that the function has a
  450. prototype declaration. Otherwise, if the target machine has two
  451. different pointer representations, the compiler won't know which
  452. representation to use for that argument. You can avoid the problem by
  453. explicitly casting the constant to the proper pointer type, but we
  454. recommend instead adding a prototype for the function you are calling.
  455. @node Important Data Types
  456. @section Important Data Types
  457. The result of subtracting two pointers in C is always an integer, but the
  458. precise data type varies from C compiler to C compiler. Likewise, the
  459. data type of the result of @code{sizeof} also varies between compilers.
  460. ISO C defines standard aliases for these two types, so you can refer to
  461. them in a portable fashion. They are defined in the header file
  462. @file{stddef.h}.
  463. @pindex stddef.h
  464. @deftp {Data Type} ptrdiff_t
  465. @standards{ISO, stddef.h}
  466. This is the signed integer type of the result of subtracting two
  467. pointers. For example, with the declaration @code{char *p1, *p2;}, the
  468. expression @code{p2 - p1} is of type @code{ptrdiff_t}. This will
  469. probably be one of the standard signed integer types (@w{@code{short
  470. int}}, @code{int} or @w{@code{long int}}), but might be a nonstandard
  471. type that exists only for this purpose.
  472. @end deftp
  473. @deftp {Data Type} size_t
  474. @standards{ISO, stddef.h}
  475. This is an unsigned integer type used to represent the sizes of objects.
  476. The result of the @code{sizeof} operator is of this type, and functions
  477. such as @code{malloc} (@pxref{Unconstrained Allocation}) and
  478. @code{memcpy} (@pxref{Copying Strings and Arrays}) accept arguments of
  479. this type to specify object sizes. On systems using @theglibc{}, this
  480. will be @w{@code{unsigned int}} or @w{@code{unsigned long int}}.
  481. @strong{Usage Note:} @code{size_t} is the preferred way to declare any
  482. arguments or variables that hold the size of an object.
  483. @end deftp
  484. @strong{Compatibility Note:} Implementations of C before the advent of
  485. @w{ISO C} generally used @code{unsigned int} for representing object sizes
  486. and @code{int} for pointer subtraction results. They did not
  487. necessarily define either @code{size_t} or @code{ptrdiff_t}. Unix
  488. systems did define @code{size_t}, in @file{sys/types.h}, but the
  489. definition was usually a signed type.
  490. @node Data Type Measurements
  491. @section Data Type Measurements
  492. Most of the time, if you choose the proper C data type for each object
  493. in your program, you need not be concerned with just how it is
  494. represented or how many bits it uses. When you do need such
  495. information, the C language itself does not provide a way to get it.
  496. The header files @file{limits.h} and @file{float.h} contain macros
  497. which give you this information in full detail.
  498. @menu
  499. * Width of Type:: How many bits does an integer type hold?
  500. * Range of Type:: What are the largest and smallest values
  501. that an integer type can hold?
  502. * Floating Type Macros:: Parameters that measure the floating point types.
  503. * Structure Measurement:: Getting measurements on structure types.
  504. @end menu
  505. @node Width of Type
  506. @subsection Width of an Integer Type
  507. @cindex integer type width
  508. @cindex width of integer type
  509. @cindex type measurements, integer
  510. @pindex limits.h
  511. TS 18661-1:2014 defines macros for the width of integer types (the
  512. number of value and sign bits). One benefit of these macros is they
  513. can be used in @code{#if} preprocessor directives, whereas
  514. @code{sizeof} cannot. The following macros are defined in
  515. @file{limits.h}.
  516. @vtable @code
  517. @item CHAR_WIDTH
  518. @itemx SCHAR_WIDTH
  519. @itemx UCHAR_WIDTH
  520. @itemx SHRT_WIDTH
  521. @itemx USHRT_WIDTH
  522. @itemx INT_WIDTH
  523. @itemx UINT_WIDTH
  524. @itemx LONG_WIDTH
  525. @itemx ULONG_WIDTH
  526. @itemx LLONG_WIDTH
  527. @itemx ULLONG_WIDTH
  528. @standards{ISO, limits.h}
  529. These are the widths of the types @code{char}, @code{signed char},
  530. @code{unsigned char}, @code{short int}, @code{unsigned short int},
  531. @code{int}, @code{unsigned int}, @code{long int}, @code{unsigned long
  532. int}, @code{long long int} and @code{unsigned long long int},
  533. respectively.
  534. @end vtable
  535. Further such macros are defined in @file{stdint.h}. Apart from those
  536. for types specified by width (@pxref{Integers}), the following are
  537. defined:
  538. @vtable @code
  539. @item INTPTR_WIDTH
  540. @itemx UINTPTR_WIDTH
  541. @itemx PTRDIFF_WIDTH
  542. @itemx SIG_ATOMIC_WIDTH
  543. @itemx SIZE_WIDTH
  544. @itemx WCHAR_WIDTH
  545. @itemx WINT_WIDTH
  546. @standards{ISO, stdint.h}
  547. These are the widths of the types @code{intptr_t}, @code{uintptr_t},
  548. @code{ptrdiff_t}, @code{sig_atomic_t}, @code{size_t}, @code{wchar_t}
  549. and @code{wint_t}, respectively.
  550. @end vtable
  551. A common reason that a program needs to know how many bits are in an
  552. integer type is for using an array of @code{unsigned long int} as a
  553. bit vector. You can access the bit at index @var{n} with:
  554. @smallexample
  555. vector[@var{n} / ULONG_WIDTH] & (1UL << (@var{n} % ULONG_WIDTH))
  556. @end smallexample
  557. Before @code{ULONG_WIDTH} was a part of the C language,
  558. @code{CHAR_BIT} was used to compute the number of bits in an integer
  559. data type.
  560. @deftypevr Macro int CHAR_BIT
  561. @standards{C90, limits.h}
  562. This is the number of bits in a @code{char}. POSIX.1-2001 requires
  563. this to be 8.
  564. @end deftypevr
  565. The number of bits in any data type @var{type} can be computed like
  566. this:
  567. @smallexample
  568. sizeof (@var{type}) * CHAR_BIT
  569. @end smallexample
  570. That expression includes padding bits as well as value and sign bits.
  571. On all systems supported by @theglibc{}, standard integer types other
  572. than @code{_Bool} do not have any padding bits.
  573. @strong{Portability Note:} One cannot actually easily compute the
  574. number of usable bits in a portable manner.
  575. @node Range of Type
  576. @subsection Range of an Integer Type
  577. @cindex integer type range
  578. @cindex range of integer type
  579. @cindex limits, integer types
  580. Suppose you need to store an integer value which can range from zero to
  581. one million. Which is the smallest type you can use? There is no
  582. general rule; it depends on the C compiler and target machine. You can
  583. use the @samp{MIN} and @samp{MAX} macros in @file{limits.h} to determine
  584. which type will work.
  585. Each signed integer type has a pair of macros which give the smallest
  586. and largest values that it can hold. Each unsigned integer type has one
  587. such macro, for the maximum value; the minimum value is, of course,
  588. zero.
  589. The values of these macros are all integer constant expressions. The
  590. @samp{MAX} and @samp{MIN} macros for @code{char} and @w{@code{short
  591. int}} types have values of type @code{int}. The @samp{MAX} and
  592. @samp{MIN} macros for the other types have values of the same type
  593. described by the macro---thus, @code{ULONG_MAX} has type
  594. @w{@code{unsigned long int}}.
  595. @comment Extra blank lines make it look better.
  596. @vtable @code
  597. @item SCHAR_MIN
  598. @standards{ISO, limits.h}
  599. This is the minimum value that can be represented by a @w{@code{signed char}}.
  600. @item SCHAR_MAX
  601. @itemx UCHAR_MAX
  602. @standards{ISO, limits.h}
  603. These are the maximum values that can be represented by a
  604. @w{@code{signed char}} and @w{@code{unsigned char}}, respectively.
  605. @item CHAR_MIN
  606. @standards{ISO, limits.h}
  607. This is the minimum value that can be represented by a @code{char}.
  608. It's equal to @code{SCHAR_MIN} if @code{char} is signed, or zero
  609. otherwise.
  610. @item CHAR_MAX
  611. @standards{ISO, limits.h}
  612. This is the maximum value that can be represented by a @code{char}.
  613. It's equal to @code{SCHAR_MAX} if @code{char} is signed, or
  614. @code{UCHAR_MAX} otherwise.
  615. @item SHRT_MIN
  616. @standards{ISO, limits.h}
  617. This is the minimum value that can be represented by a @w{@code{signed
  618. short int}}. On most machines that @theglibc{} runs on,
  619. @code{short} integers are 16-bit quantities.
  620. @item SHRT_MAX
  621. @itemx USHRT_MAX
  622. @standards{ISO, limits.h}
  623. These are the maximum values that can be represented by a
  624. @w{@code{signed short int}} and @w{@code{unsigned short int}},
  625. respectively.
  626. @item INT_MIN
  627. @standards{ISO, limits.h}
  628. This is the minimum value that can be represented by a @w{@code{signed
  629. int}}. On most machines that @theglibc{} runs on, an @code{int} is
  630. a 32-bit quantity.
  631. @item INT_MAX
  632. @itemx UINT_MAX
  633. @standards{ISO, limits.h}
  634. These are the maximum values that can be represented by, respectively,
  635. the type @w{@code{signed int}} and the type @w{@code{unsigned int}}.
  636. @item LONG_MIN
  637. @standards{ISO, limits.h}
  638. This is the minimum value that can be represented by a @w{@code{signed
  639. long int}}. On most machines that @theglibc{} runs on, @code{long}
  640. integers are 32-bit quantities, the same size as @code{int}.
  641. @item LONG_MAX
  642. @itemx ULONG_MAX
  643. @standards{ISO, limits.h}
  644. These are the maximum values that can be represented by a
  645. @w{@code{signed long int}} and @code{unsigned long int}, respectively.
  646. @item LLONG_MIN
  647. @standards{ISO, limits.h}
  648. This is the minimum value that can be represented by a @w{@code{signed
  649. long long int}}. On most machines that @theglibc{} runs on,
  650. @w{@code{long long}} integers are 64-bit quantities.
  651. @item LLONG_MAX
  652. @itemx ULLONG_MAX
  653. @standards{ISO, limits.h}
  654. These are the maximum values that can be represented by a @code{signed
  655. long long int} and @code{unsigned long long int}, respectively.
  656. @item LONG_LONG_MIN
  657. @itemx LONG_LONG_MAX
  658. @itemx ULONG_LONG_MAX
  659. @standards{GNU, limits.h}
  660. These are obsolete names for @code{LLONG_MIN}, @code{LLONG_MAX}, and
  661. @code{ULLONG_MAX}. They are only available if @code{_GNU_SOURCE} is
  662. defined (@pxref{Feature Test Macros}). In GCC versions prior to 3.0,
  663. these were the only names available.
  664. @item WCHAR_MAX
  665. @standards{GNU, limits.h}
  666. This is the maximum value that can be represented by a @code{wchar_t}.
  667. @xref{Extended Char Intro}.
  668. @end vtable
  669. The header file @file{limits.h} also defines some additional constants
  670. that parameterize various operating system and file system limits. These
  671. constants are described in @ref{System Configuration}.
  672. @node Floating Type Macros
  673. @subsection Floating Type Macros
  674. @cindex floating type measurements
  675. @cindex measurements of floating types
  676. @cindex type measurements, floating
  677. @cindex limits, floating types
  678. The specific representation of floating point numbers varies from
  679. machine to machine. Because floating point numbers are represented
  680. internally as approximate quantities, algorithms for manipulating
  681. floating point data often need to take account of the precise details of
  682. the machine's floating point representation.
  683. Some of the functions in the C library itself need this information; for
  684. example, the algorithms for printing and reading floating point numbers
  685. (@pxref{I/O on Streams}) and for calculating trigonometric and
  686. irrational functions (@pxref{Mathematics}) use it to avoid round-off
  687. error and loss of accuracy. User programs that implement numerical
  688. analysis techniques also often need this information in order to
  689. minimize or compute error bounds.
  690. The header file @file{float.h} describes the format used by your
  691. machine.
  692. @menu
  693. * Floating Point Concepts:: Definitions of terminology.
  694. * Floating Point Parameters:: Details of specific macros.
  695. * IEEE Floating Point:: The measurements for one common
  696. representation.
  697. @end menu
  698. @node Floating Point Concepts
  699. @subsubsection Floating Point Representation Concepts
  700. This section introduces the terminology for describing floating point
  701. representations.
  702. You are probably already familiar with most of these concepts in terms
  703. of scientific or exponential notation for floating point numbers. For
  704. example, the number @code{123456.0} could be expressed in exponential
  705. notation as @code{1.23456e+05}, a shorthand notation indicating that the
  706. mantissa @code{1.23456} is multiplied by the base @code{10} raised to
  707. power @code{5}.
  708. More formally, the internal representation of a floating point number
  709. can be characterized in terms of the following parameters:
  710. @itemize @bullet
  711. @item
  712. @cindex sign (of floating point number)
  713. The @dfn{sign} is either @code{-1} or @code{1}.
  714. @item
  715. @cindex base (of floating point number)
  716. @cindex radix (of floating point number)
  717. The @dfn{base} or @dfn{radix} for exponentiation, an integer greater
  718. than @code{1}. This is a constant for a particular representation.
  719. @item
  720. @cindex exponent (of floating point number)
  721. The @dfn{exponent} to which the base is raised. The upper and lower
  722. bounds of the exponent value are constants for a particular
  723. representation.
  724. @cindex bias (of floating point number exponent)
  725. Sometimes, in the actual bits representing the floating point number,
  726. the exponent is @dfn{biased} by adding a constant to it, to make it
  727. always be represented as an unsigned quantity. This is only important
  728. if you have some reason to pick apart the bit fields making up the
  729. floating point number by hand, which is something for which @theglibc{}
  730. provides no support. So this is ignored in the discussion that
  731. follows.
  732. @item
  733. @cindex mantissa (of floating point number)
  734. @cindex significand (of floating point number)
  735. The @dfn{mantissa} or @dfn{significand} is an unsigned integer which is a
  736. part of each floating point number.
  737. @item
  738. @cindex precision (of floating point number)
  739. The @dfn{precision} of the mantissa. If the base of the representation
  740. is @var{b}, then the precision is the number of base-@var{b} digits in
  741. the mantissa. This is a constant for a particular representation.
  742. @cindex hidden bit (of floating point number mantissa)
  743. Many floating point representations have an implicit @dfn{hidden bit} in
  744. the mantissa. This is a bit which is present virtually in the mantissa,
  745. but not stored in memory because its value is always 1 in a normalized
  746. number. The precision figure (see above) includes any hidden bits.
  747. Again, @theglibc{} provides no facilities for dealing with such
  748. low-level aspects of the representation.
  749. @end itemize
  750. The mantissa of a floating point number represents an implicit fraction
  751. whose denominator is the base raised to the power of the precision. Since
  752. the largest representable mantissa is one less than this denominator, the
  753. value of the fraction is always strictly less than @code{1}. The
  754. mathematical value of a floating point number is then the product of this
  755. fraction, the sign, and the base raised to the exponent.
  756. @cindex normalized floating point number
  757. We say that the floating point number is @dfn{normalized} if the
  758. fraction is at least @code{1/@var{b}}, where @var{b} is the base. In
  759. other words, the mantissa would be too large to fit if it were
  760. multiplied by the base. Non-normalized numbers are sometimes called
  761. @dfn{denormal}; they contain less precision than the representation
  762. normally can hold.
  763. If the number is not normalized, then you can subtract @code{1} from the
  764. exponent while multiplying the mantissa by the base, and get another
  765. floating point number with the same value. @dfn{Normalization} consists
  766. of doing this repeatedly until the number is normalized. Two distinct
  767. normalized floating point numbers cannot be equal in value.
  768. (There is an exception to this rule: if the mantissa is zero, it is
  769. considered normalized. Another exception happens on certain machines
  770. where the exponent is as small as the representation can hold. Then
  771. it is impossible to subtract @code{1} from the exponent, so a number
  772. may be normalized even if its fraction is less than @code{1/@var{b}}.)
  773. @node Floating Point Parameters
  774. @subsubsection Floating Point Parameters
  775. @pindex float.h
  776. These macro definitions can be accessed by including the header file
  777. @file{float.h} in your program.
  778. Macro names starting with @samp{FLT_} refer to the @code{float} type,
  779. while names beginning with @samp{DBL_} refer to the @code{double} type
  780. and names beginning with @samp{LDBL_} refer to the @code{long double}
  781. type. (If GCC does not support @code{long double} as a distinct data
  782. type on a target machine then the values for the @samp{LDBL_} constants
  783. are equal to the corresponding constants for the @code{double} type.)
  784. Of these macros, only @code{FLT_RADIX} is guaranteed to be a constant
  785. expression. The other macros listed here cannot be reliably used in
  786. places that require constant expressions, such as @samp{#if}
  787. preprocessing directives or in the dimensions of static arrays.
  788. Although the @w{ISO C} standard specifies minimum and maximum values for
  789. most of these parameters, the GNU C implementation uses whatever values
  790. describe the floating point representation of the target machine. So in
  791. principle GNU C actually satisfies the @w{ISO C} requirements only if the
  792. target machine is suitable. In practice, all the machines currently
  793. supported are suitable.
  794. @vtable @code
  795. @item FLT_ROUNDS
  796. @standards{C90, float.h}
  797. This value characterizes the rounding mode for floating point addition.
  798. The following values indicate standard rounding modes:
  799. @need 750
  800. @table @code
  801. @item -1
  802. The mode is indeterminable.
  803. @item 0
  804. Rounding is towards zero.
  805. @item 1
  806. Rounding is to the nearest number.
  807. @item 2
  808. Rounding is towards positive infinity.
  809. @item 3
  810. Rounding is towards negative infinity.
  811. @end table
  812. @noindent
  813. Any other value represents a machine-dependent nonstandard rounding
  814. mode.
  815. On most machines, the value is @code{1}, in accordance with the IEEE
  816. standard for floating point.
  817. Here is a table showing how certain values round for each possible value
  818. of @code{FLT_ROUNDS}, if the other aspects of the representation match
  819. the IEEE single-precision standard.
  820. @smallexample
  821. 0 1 2 3
  822. 1.00000003 1.0 1.0 1.00000012 1.0
  823. 1.00000007 1.0 1.00000012 1.00000012 1.0
  824. -1.00000003 -1.0 -1.0 -1.0 -1.00000012
  825. -1.00000007 -1.0 -1.00000012 -1.0 -1.00000012
  826. @end smallexample
  827. @item FLT_RADIX
  828. @standards{C90, float.h}
  829. This is the value of the base, or radix, of the exponent representation.
  830. This is guaranteed to be a constant expression, unlike the other macros
  831. described in this section. The value is 2 on all machines we know of
  832. except the IBM 360 and derivatives.
  833. @item FLT_MANT_DIG
  834. @standards{C90, float.h}
  835. This is the number of base-@code{FLT_RADIX} digits in the floating point
  836. mantissa for the @code{float} data type. The following expression
  837. yields @code{1.0} (even though mathematically it should not) due to the
  838. limited number of mantissa digits:
  839. @smallexample
  840. float radix = FLT_RADIX;
  841. 1.0f + 1.0f / radix / radix / @dots{} / radix
  842. @end smallexample
  843. @noindent
  844. where @code{radix} appears @code{FLT_MANT_DIG} times.
  845. @item DBL_MANT_DIG
  846. @itemx LDBL_MANT_DIG
  847. @standards{C90, float.h}
  848. This is the number of base-@code{FLT_RADIX} digits in the floating point
  849. mantissa for the data types @code{double} and @code{long double},
  850. respectively.
  851. @comment Extra blank lines make it look better.
  852. @item FLT_DIG
  853. @standards{C90, float.h}
  854. This is the number of decimal digits of precision for the @code{float}
  855. data type. Technically, if @var{p} and @var{b} are the precision and
  856. base (respectively) for the representation, then the decimal precision
  857. @var{q} is the maximum number of decimal digits such that any floating
  858. point number with @var{q} base 10 digits can be rounded to a floating
  859. point number with @var{p} base @var{b} digits and back again, without
  860. change to the @var{q} decimal digits.
  861. The value of this macro is supposed to be at least @code{6}, to satisfy
  862. @w{ISO C}.
  863. @item DBL_DIG
  864. @itemx LDBL_DIG
  865. @standards{C90, float.h}
  866. These are similar to @code{FLT_DIG}, but for the data types
  867. @code{double} and @code{long double}, respectively. The values of these
  868. macros are supposed to be at least @code{10}.
  869. @item FLT_MIN_EXP
  870. @standards{C90, float.h}
  871. This is the smallest possible exponent value for type @code{float}.
  872. More precisely, it is the minimum negative integer such that the value
  873. @code{FLT_RADIX} raised to this power minus 1 can be represented as a
  874. normalized floating point number of type @code{float}.
  875. @item DBL_MIN_EXP
  876. @itemx LDBL_MIN_EXP
  877. @standards{C90, float.h}
  878. These are similar to @code{FLT_MIN_EXP}, but for the data types
  879. @code{double} and @code{long double}, respectively.
  880. @item FLT_MIN_10_EXP
  881. @standards{C90, float.h}
  882. This is the minimum negative integer such that @code{10} raised to this
  883. power minus 1 can be represented as a normalized floating point number
  884. of type @code{float}. This is supposed to be @code{-37} or even less.
  885. @item DBL_MIN_10_EXP
  886. @itemx LDBL_MIN_10_EXP
  887. @standards{C90, float.h}
  888. These are similar to @code{FLT_MIN_10_EXP}, but for the data types
  889. @code{double} and @code{long double}, respectively.
  890. @item FLT_MAX_EXP
  891. @standards{C90, float.h}
  892. This is the largest possible exponent value for type @code{float}. More
  893. precisely, this is the maximum positive integer such that value
  894. @code{FLT_RADIX} raised to this power minus 1 can be represented as a
  895. floating point number of type @code{float}.
  896. @item DBL_MAX_EXP
  897. @itemx LDBL_MAX_EXP
  898. @standards{C90, float.h}
  899. These are similar to @code{FLT_MAX_EXP}, but for the data types
  900. @code{double} and @code{long double}, respectively.
  901. @item FLT_MAX_10_EXP
  902. @standards{C90, float.h}
  903. This is the maximum positive integer such that @code{10} raised to this
  904. power minus 1 can be represented as a normalized floating point number
  905. of type @code{float}. This is supposed to be at least @code{37}.
  906. @item DBL_MAX_10_EXP
  907. @itemx LDBL_MAX_10_EXP
  908. @standards{C90, float.h}
  909. These are similar to @code{FLT_MAX_10_EXP}, but for the data types
  910. @code{double} and @code{long double}, respectively.
  911. @item FLT_MAX
  912. @standards{C90, float.h}
  913. The value of this macro is the maximum number representable in type
  914. @code{float}. It is supposed to be at least @code{1E+37}. The value
  915. has type @code{float}.
  916. The smallest representable number is @code{- FLT_MAX}.
  917. @item DBL_MAX
  918. @itemx LDBL_MAX
  919. @standards{C90, float.h}
  920. These are similar to @code{FLT_MAX}, but for the data types
  921. @code{double} and @code{long double}, respectively. The type of the
  922. macro's value is the same as the type it describes.
  923. @item FLT_MIN
  924. @standards{C90, float.h}
  925. The value of this macro is the minimum normalized positive floating
  926. point number that is representable in type @code{float}. It is supposed
  927. to be no more than @code{1E-37}.
  928. @item DBL_MIN
  929. @itemx LDBL_MIN
  930. @standards{C90, float.h}
  931. These are similar to @code{FLT_MIN}, but for the data types
  932. @code{double} and @code{long double}, respectively. The type of the
  933. macro's value is the same as the type it describes.
  934. @item FLT_EPSILON
  935. @standards{C90, float.h}
  936. This is the difference between 1 and the smallest floating point
  937. number of type @code{float} that is greater than 1. It's supposed to
  938. be no greater than @code{1E-5}.
  939. @item DBL_EPSILON
  940. @itemx LDBL_EPSILON
  941. @standards{C90, float.h}
  942. These are similar to @code{FLT_EPSILON}, but for the data types
  943. @code{double} and @code{long double}, respectively. The type of the
  944. macro's value is the same as the type it describes. The values are not
  945. supposed to be greater than @code{1E-9}.
  946. @end vtable
  947. @node IEEE Floating Point
  948. @subsubsection IEEE Floating Point
  949. @cindex IEEE floating point representation
  950. @cindex floating point, IEEE
  951. Here is an example showing how the floating type measurements come out
  952. for the most common floating point representation, specified by the
  953. @cite{IEEE Standard for Binary Floating Point Arithmetic (ANSI/IEEE Std
  954. 754-1985)}. Nearly all computers designed since the 1980s use this
  955. format.
  956. The IEEE single-precision float representation uses a base of 2. There
  957. is a sign bit, a mantissa with 23 bits plus one hidden bit (so the total
  958. precision is 24 base-2 digits), and an 8-bit exponent that can represent
  959. values in the range -125 to 128, inclusive.
  960. So, for an implementation that uses this representation for the
  961. @code{float} data type, appropriate values for the corresponding
  962. parameters are:
  963. @smallexample
  964. FLT_RADIX 2
  965. FLT_MANT_DIG 24
  966. FLT_DIG 6
  967. FLT_MIN_EXP -125
  968. FLT_MIN_10_EXP -37
  969. FLT_MAX_EXP 128
  970. FLT_MAX_10_EXP +38
  971. FLT_MIN 1.17549435E-38F
  972. FLT_MAX 3.40282347E+38F
  973. FLT_EPSILON 1.19209290E-07F
  974. @end smallexample
  975. Here are the values for the @code{double} data type:
  976. @smallexample
  977. DBL_MANT_DIG 53
  978. DBL_DIG 15
  979. DBL_MIN_EXP -1021
  980. DBL_MIN_10_EXP -307
  981. DBL_MAX_EXP 1024
  982. DBL_MAX_10_EXP 308
  983. DBL_MAX 1.7976931348623157E+308
  984. DBL_MIN 2.2250738585072014E-308
  985. DBL_EPSILON 2.2204460492503131E-016
  986. @end smallexample
  987. @node Structure Measurement
  988. @subsection Structure Field Offset Measurement
  989. You can use @code{offsetof} to measure the location within a structure
  990. type of a particular structure member.
  991. @deftypefn {Macro} size_t offsetof (@var{type}, @var{member})
  992. @standards{ISO, stddef.h}
  993. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  994. @c This is no longer provided by glibc, but rather by the compiler.
  995. This expands to an integer constant expression that is the offset of the
  996. structure member named @var{member} in the structure type @var{type}.
  997. For example, @code{offsetof (struct s, elem)} is the offset, in bytes,
  998. of the member @code{elem} in a @code{struct s}.
  999. This macro won't work if @var{member} is a bit field; you get an error
  1000. from the C compiler in that case.
  1001. @end deftypefn