C Standard Library source code anatomy (1): Type-related definitions

Source: Internet
Author: User
Tags define null float number

Note: The entire c Standard Library anatomy series environment is Ubuntu 8.04, the compiler is GCC 4.2.4, because the Linux system only has c Standard Library header files (under/usr/include ), the function library is compiled into a library without source code. Therefore, glibc 2.9 is used for source code anatomy and can be downloaded from the GNU official site.
Types include limits. H, float. H, stddef. H, stdbool. H, stdarg. H, iso646.h, and stdint. h. Except stdint. H, the remaining six files are in the/usr/lib/GCC/i486-linux-gnu/4.2.4/include directory of the GCC compiler. Stdint. H is introduced in/usr/include in c99. It provides the basic definition of extended integers, which can be further dissected.
1. Limits. h:
Defines the range of integer types. Limits. H is also available in/usr/include. It defines the range of each integer type first, so that these values can be used when you do not need GCC to build your program. If you use the GCC compiler to build your program, the GCC compiler's own limits. H will be used (the previous definition will be # UNDEF off ). Because this limits. H will use the built-in limits. h of GCC, We will dissect limits. h under/usr/include.

/* ISO c99 standard: 7.10/5.2.4.2.1 integer <limits. h> */<br/> # ifndef _ libc_limits_h _ <br/> # DEFINE _ libc_limits_h_1 <br/> # include <features. h>/* macro, for example, the isoc99 option, POSIX option, and xopen option */<br/> # define mb_len_max16/* supports 16-byte characters in the Region */<br/> /* when gnu cc is not used, all the following symbols must be defined, otherwise, use the definition in the GCC compiler (see the following) */<br/> # If! Defined _ gnuc _ | _ gnuc _ <2 <br/> # ifndef _ limits_h <br/> # DEFINE _ limits_h1 <br/> # include <bits/ wordsize. h>/* defines the _ wordsize macro that represents the number of digits of a word, 64-bit platform values: 64, 32-bit platform values: 32 */<br/> # define char_bit8/* char type width: 8-bit */<br/> # define schar_min (-128) /* the minimum value of signed Char is-2 ^ 7, the complement code is 10000000, and there is no corresponding positive number, the counter number is still self */<br/> # The maximum value of define schar_max127/* signed Char is 2 ^ 7-1 = 01111111 */<br/> # define uchar_max255/* unsigend char The maximum value is 2 ^ 8-1 = 11111111 (the minimum value is 0) */<br/> # ifdef _ char_unsigned _/* determine whether char = unsigned char or char = signed Char */<br/> # define char_min0 <br/> # define char_maxuchar_max <br/> # else <br/> # define char_minschar_min/* This is used in GCC, that is, char = signed Char */<br/> # define char_maxschar_max <br/> # endif <br/> # define shrt_min (-32768) /* the minimum value of signed short Int Is-2 ^ 15 */<br/> # define shrt_max32767/* signed shor The minimum value of t int is 2 ^ 15-1 */<br/> # define ushrt_max65535/* unsigned short Int. The maximum value is 2 ^ 16-1 (the minimum value is 0) */<br/> # define int_min (-int_max-1) /* the minimum value of Int Is-2 ^ 31 */<br/> # define int_max2147483647/* the maximum value of Int Is 2 ^ 31-1 */<br/> # define uint_max4294967295u/ * The maximum unsigned int value is 2 ^ 32-1 (the minimum value is 0) */<br/> # If _ wordsize = 64/* 64-bit X86 platform, the macro is in <bits/wordsize. h> medium */<br/> # define long_max9223372042554775807l/* signed long int Region: 2 ^ 63-1 */<br/> # else/* 32-bit X86 platform */<br/> # define long_max2147483647l/* signed long int; maximum value: 2 ^ 31-1 */<br/> # endif <br/> # define long_min (-long_max-1l) /* the minimum value of signed long Int Is-2 ^ 31 */<br/> # If _ wordsize = 64 <br/> # define ulong_max18446721373709551615ul/* 64 platform: the maximum unsigend long int value is 2 ^ 64-1 */<br/> # else <br/> # define ulong_max4294967295ul/* 32-bit platform: the maximum unsigned long int value is 2 ^ 32-1 */<br/> # End If <br/> # ifdef _ use_isoc99/* <feature. h> zhonghong: long long type is introduced by the c99 standard */<br/> # define llong_max9223372042554775807ll/* signed long Int. The maximum value is 2 ^ 63-1 */<br/> # define llong_min (-llong_max-1ll) /* signed long int minimum value:-2 ^ 63 */<br/> # define ullong_max18446731673709551615ull/* unsigned long int maximum value: 2 ^ 64-1 */<br/> # endif/* ISO c99 */<br/> # endif/* limits. H */<br/> # endif/* GCC 2. */<br/> # Endif /*! _ Libc_limits_h _ */<br/>/* obtain the limits of the compiler. h, which defines almost all ISO constants */<br/> # If defined _ gnuc __&&! Defined _ gcc_limits_h _/* _ gcc_limits_h _ is the GCC file definition */<br/> # define de_next <limits. h> <br/> # endif <br/>/* GCC version <limits. h> llong_min, llong_max, and ullong_max are not defined, define */<br/> # If defined _ use_isoc99 & defined _ gnuc __< br/> # ifndef llong_min <br/> # define llong_min (- LLONG_MAX-1) <br/> # endif <br/> # ifndef llong_max <br/> # define llong_maxcompute long_long_max __< br/> # endif <br/> # ifndef ullong_max <br/> # define ullong_max (llong_max * 2ull + 1) <br/> # endif <br/> # ifdef _ use_posix <br/>/* add content to <limits. h> medium */<br/> # include <bits/posixw.lim.h> <br/> # endif <br/> # ifdef _ use_posix2 <br/> # include <bits/ posix2_lim.h> <br/> # endif <br/> # ifdef _ use_xopen <br/> # include <bits/xopen_lim.h> <br/> # endif

Explanation:
(1)/usr/include/limits. in the implementation of H, char = unsigned char occupies 8 places, short occupies 16 places, int occupies 32 places, and long occupies 64 places on 64-bit platforms, the 32-bit platform occupies 32 bits, and the long standard introduced by c99 occupies 64 bits. Both of them are singed and unsigned. The default values are signed ). A signed integer is generally represented by a binary complement in most architectures (of course, the C standard also supports other encodings). That is, positive numbers are directly encoded and the symbol bit is 0; negative number indicates that the corresponding positive number is reversed and then 1 is added, and the symbol bit is 1. The signed integer range is-2 ** (n-1 )~ 2 ** (n-1)-1, where the minimum negative number-2 ** (n-1) = 100... 0 does not correspond to a positive number, and its inverse number is still itself. Unsigned integers are directly binary encoded in the range of 0 ~ 2 ** n-1. If the GCC limits. h is used, the value of each macro depends on the built-in definition of the GCC compiler, which is generally the same as the value here.
(2) uchar_max must be equal to 2 ** CHAR_BIT-1, and the signed integer generally has min =-MAX-1.
(3) The feature. h file defines some macros that indicate compilation options, such as the isoc99 option, POSIX option, and xopen option. Bits/wordsize. h defines the _ wordsize macro that represents the number of digits of a word. The 64-bit platform value is 64, and the 32-bit platform value is 32. They are all under/usr/include.
(4) If you want to comply with the c99 standard, you need to define llong_min, llong_max, and ullong_max in some GCC versions. If POSIX is used, add some POSIX items.
2. Float. h:
Defines features of the floating point type.

/* Iso c Standard: 5.2.4.2.2 floating point type feature <float. h> */<br/> # ifndef _ float_h ___ <br/> # DEFINE _ float_h ___ <br/>/* Real-number floating-point representation: <br/> X = S * (B ** e) * [F1 * B ** (-1) + F2 * B ** (-2) +... + FP * B ** (-p)], Emin <= e <= Emax, (** indicates power) <br/> S is the symbol (+ 1 or-1) <br/> B is the base number (usually 2, 8, 16) <br/> E is a numeric value, the value range is between Emin and Emax. <br/> P indicates the number of valid bits in B notation. <br/> FK indicates a valid number, 0 <= FK <B <br/> */</P> <p>/* B: base number of exponential notation */<br/> # UNDEF flt_radix <br/> # define flt_radix1_flt_radix _ _/* Base B, generally 2, applicable to all three floating point types */<br/>/* P: it is the number of bits in hexadecimal notation */<br/> # UNDEF flt_mant_dig <br/> # UNDEF dbl_mant_dig <br/> # UNDEF ldbl_mant_dig <br/> # define interval __/ * float's B-digit valid bits p */<br/> # define dbl_mant_dig1_dbl_mant_dig _/* double's B-digit valid bits p */<br/> # define ldbl_mant_dig1_ldbl_mant_dig _ _/* long double's B-digit valid digits p */<br/>/* Q: precision decimal places. So that any floating point number with Q decimal places can be rounded to a floating point with p B-digit digits, and the Q decimal places can be converted back without changing <br/>. Q: <br/> P * log10 (B) If B is 10 power <br/> floor (P-1) * log10 (B )) otherwise, <br/> */<br/> # UNDEF flt_dig <br/> # UNDEF dbl_dig <br/> # UNDEF ldbl_dig <br/> # define flt_dig1_flt_dig _/* float number of decimal places, usually 6 */<br/> # define dbl_dig1_dbl_dig _/* Double decimal places, usually 10 */<br/> # define ldbl_dig1_ldbl_dig _/* Long Double Precision decimal places, usually 10 */<br/>/* Emin: minimum negative integer x, make B ** (x-1) within the normalized floating point number type value range */<br/> # UNDEF flt_min_exp <br/> # UNDEF DBL _ Min_exp <br/> # UNDEF ldbl_min_exp <br/> # define metadata _/* float Emin */<br/> # define dbl_min_exp1_dbl_min_exp _/* Double Emin */ <br/> # define ldbl_min_exp1_ldbl_min_exp _/* long double Emin */<br/>/* Ceil (log10 (B) * (emin-1): minimum negative integer x, so that 10 ** (x-1) within the <br/> normalized floating point number type value range, the general value is-37 <br/> */<br/> # UNDEF flt_min_10_exp <br/> # UNDEF dbl_min_10_exp <br/> # UNDEF ldbl_min_10_exp <br/> # define flt_m In_10_exp1_flt_min_10_exp __< br/> # define dbl_min_10_exp1_dbl_min_10_exp __< br/> # define __< br/>/* Emax: maximum integer x, making B * (x-1) */<br/> # UNDEF flt_max_exp <br/> # UNDEF dbl_max_exp <br/> # UNDEF ldbl_max_exp <br/> # define flt_max_exp1_flt_max_exp _ _/* float Emax */<br/> # define dbl_max_exp1_dbl_max_exp _/* Double Emax */<br/> # define ldbl_max_exp1_ldbl_max_exp __/ * Long Double Emax */<br/>/* floor (log10 (1-B **-p) * B ** Emax): maximum integer x, make 10 ** X within the range of a finite floating point number that can be expressed as <br/>, generally, the value is 37 <br/> */<br/> # UNDEF flt_max_10_exp <br/> # UNDEF dbl_max_10_exp <br/> # UNDEF ldbl_max_10_exp <br/> # define __< br/> # define dbl_max_10_exp1_dbl_max_10_exp __< br/> # define ldbl_max_10_exp1_ldbl_max_10_exp __< br/>/* (1-B **-P) * B ** Emax: The maximum finite floating point number that can be expressed. The general value is 10 ** 37 */<br/> # und EF flt_max <br/> # UNDEF dbl_max <br/> # UNDEF ldbl_max <br/> # define flt_maxcompute flt_max __< br/> # define dbl_maxcompute dbl_max __< br/> # define __ <br/>/* B ** (1-p): The smallest x (x> 0.0), so that 1.0 + x> 1.0, macro is defined as the maximum allowed value, for example, <br/> float is 10 **-5, set double and long double to 10 **-9 */<br/> # UNDEF flt_epsilon <br/> # UNDEF dbl_epsilon <br/> # UNDEF ldbl_epsilon <br/> # define flt_epsilon1_flt_epsilon __< br/> # define dbl_epsilon1_dbl_epsi Lon __< br/> # define ldbl_epsilon1_ldbl_epsilon __< br/>/* B ** (Emin-1): Minimum normalized positive number, the general value is 10 **-37 */<br/> # UNDEF flt_min <br/> # UNDEF dbl_min <br/> # UNDEF ldbl_min <br/> # define flt_min1_flt_min __< br/> # define dbl_min1_dbl_min __< br/> # define ldbl_min1_ldbl_min __< br/>/* rounding method: 0: round to 0; 1: round recently; 2: round to positive infinity; 3: round to negative infinity D;-1: uncertain, <br/> applicable to all three floating point types */<br/> # UNDEF flt_rounds <br/> # define flt_rounds 1/* use the nearest Rounding Method */<br/> # I F defined (_ stdc_version _) & _ stdc_version _> = 199901l <br/>/* floating point expression Evaluate Method (introduced in c99 ): <br/>-1 uncertain <br/> the accuracy and value range of the original type are maintained when the value is 0. <br/> 1. When the value is evaluated, the float type and double type are unified with the double type. type; the long double type remains unchanged. <br/> 2. The long double type is used for evaluation. <br/> */<br/> # UNDEF flt_eval_method <br/> # define flt_eval_method1_flt_eval_method __< br/>/* n (introduced in c99): number of decimal places, so that any type of the widest floating point number with a Pmax bhexadecimal digit can be rounded down to a floating point number with N decimal places, and can be converted back without changing this value. The value of N is: <br/> Pmax * log10 (B) If B is a 10 power <br/> Ceil (1 + Pmax * log10 (B )) otherwise, <br/> */<br/> # UNDEF decimal_dig <br/> # define decimal_dig1_decimal_dig _/* the usual value is 10 */<br/> # endif /* c99 */<br/> # endif/* _ float_h ___*/

Explanation:
(1) The floating point is in the form of X = S * (B ** e) * [F1 * B ** (-1) + F2 * B ** (-2) +... + FP * B ** (-p)], Emin <= e <= Emax (** indicates power ). S indicates the symbol bit, B indicates the base number, e indicates the value, P indicates the valid digits of B, and 0 <= FK <B.
(2) floating point representation of IEEE: the float type with single precision has one-bit symbol bit s, eight-bit index E, and 23-bit ending number M. Convert to value v = (-1) ** S * 1. M * 2 ** (E-127 ). For example, 16.5 = 00010000.1 = 1.00001*2 ** 4 (becoming a normalized number), the sign bit is 0, and the index bit is 4 + 127 = 131 = 10000011 (because the index can be negative, the range of 8-Bit Signed numbers is-128 ~ 127. To use the unsigned number in a unified manner, add 127) and the ending number is 00001000000000000000000. After splicing, 16.5 of the memory is 01000001100001000000000000000000.
(3) commonly used macros include flt_dig, dbl_dig, ldbl_dig, flt_min, dbl_min, ldbl_min, flt_max, dbl_max, and ldbl_max.
3. stddef. h:
Ptrdiff_t, size_t, wchar_t, wint_t, and offsetof are defined. There are a lot of Conditional compilation macros compatible with different platforms, which is useless to us.

/* Iso c Standard: 7.17 some general definitions <stddef. h> */<br/> typedef _ ptrdiff_type _ ptrdiff_t;/* defines the ptrdiff_t of the signed integer type. Here _ ptrdiff_type __< br/> depends on the platform, it is usually of the long type */<br/> # If! (Defined (_ gnug _) & defined (size_t) <br/> typedef _ size_type _ size_t;/* defines the unsigned integer size_t, __size_type __< br/> depends on the platform, usually unsigned Int or unsigned long type */<br/> # ifdef _ BEOs __< br/> typedef long ssize_t; /* only used in BEOs */<br/> # endif <br/> typedef _ bsd_rune_t _ rune_t; /* used only for the old BSD system */<br/> # ifndef _ rune_t_declared <br/> typedef _ rune_t; /* only used in FreeBSD system */<br/> # DEFINE _ rune_t_declared <br/> # endif <br/> # ifndef _ wchar_type __< br/> # DEFINE _ _ wchar_type _ int <br/> # endif <br/> # wchar_t must be defined in ifndef _ cplusplus/* C, in C ++, wchar_t is of the built-in type */<br/> typedef _ wchar_type _ wchar_t;/* wchar_t is also of the wide character type in stddef. defined in H, here is the int type */<br/> # endif <br/> # ifndef _ wint_type __< br/> # DEFINE _ wint_type _ unsigned int <br/> # endif <br/> typedef _ wint_type _ wint_t; /* used for the unsigned wide character type */<br/> # ifndef _ cplusplus <br/> # define null (void *) 0) /* define NULL pointer constant in C as (void *) 0 */<br/> # else <br/> # define null 0/* C ++ defines a null pointer constant as 0 */<br/> # endif <br/> # ifdef _ stddef_h <br/>/* Number of address offset bytes of structure members, type Structure Type. member is a member */<br/> # define offsetof (type, member) _ builtin_offsetof (type, member) <br/> # endif

Ptrdiff_t is the signed integer obtained from the subtraction of two pointers, which is generally represented by the long type. Size_t is an unsigned integer produced by sizeof operations. It is generally represented by an unsigned Int or unsigned long. The wide character type wchar_t is also defined in stddef. H. Here it is the int type. Wint_t is used in the unsigned wide character type. Here it is the unsigned int type. The offsetof macro is used to calculate the number of address offset bytes of a structure member.
4. stdbool. h:
Is added in c99, defines the Boolean Type bool, and its two constants false = 0, true = 1. _ Bool_true_false_are_defined = 1 indicates whether the boolean type definition is complete. These definitions are consistent with those in C ++. Therefore, the standard C ++ does not need to support stdbool. h, but GCC provides this extension to support <stdbool. h>.

/* Iso c Standard: 7.16 boolean type and its value <stdbool. h> <br/> */<br/> # ifndef _ stdbool_h <br/> # DEFINE _ stdbool_h <br/> # ifndef _ cplusplus <br/> # define bool_bool /* C */<br/> # define true1 <br/> # define false0 <br/> # else/* C ++ medium */<br/> /* <stdbool. h> is an extension of GCC */<br/> # DEFINE _ boolbool <br/> # define falsefalse <br/> # define truetrue <br /># endif/* _ cplusplus */<br/>/* indicates the signal indicating whether the Boolean definition is complete */<br/> # DEFINE _ bool_true_false_are_defined1 <br/> # endif/* stdbool. H */

5. stdarg. h:
Type and Function of the variable parameter table (use macro to implement ). When you need to write a function with a variable parameter table, such as myfunc (int * ,...), you can use stdarg. h to traverse "... to complete the function. Skip a lot of useless Conditional compilation macros for compatibility with different platforms, as shown below:
/* Iso c Standard: 7.15 variable parameter table <stdarg. h> */<br/> # ifndef _ gnuc_va_list <br/> # DEFINE _ gnuc_va_list <br/> typedef _ builtin_va_list _ gnuc_va_list; /* DEFINE _ gnuc_va_list. */<br/> # endif <br/> # ifdef _ stdarg_h <br/>/* var_start (v, L ): initialize the status variable v */<br/> # define va_start (v, L) _ builtin_va_start (v, L) used to traverse the parameter table) <br/>/* terminate the traversal of the parameter table and perform necessary operations on V and the parameter table */<br/> # define va_end (v) _ builtin_va_end (V) <br/>/* return the parameter value currently pointed to by V */<br /> # Define va_arg (v, L) _ builtin_va_arg (v, L) <br/> # If! Defined (_ strict_ansi _) | _ stdc_version _ + 0 >=199900l <br/>/* this function is introduced in c99: Copy s to D, generate the second pointer to the current parameter */<br/> # define va_copy (D, S) _ builtin_va_copy (D, S) <br/> # endif <br/> # DEFINE _ va_copy (D, S) _ builtin_va_copy (D, S) <br/> typedef _ gnuc_va_list va_list; /* defines the va_list type. This type is used to define the variable state variable for traversing the variable <br/> parameter table */</P> <p> # endif/* _ stdarg_h */
Explanation:
(1) va_list type: This type is used to define the State Variable ap that traverses the variable parameter list.
(2) va_start (AP, LT): Let the internal pointer of the AP point to the first variable parameter. Use lt to specify the last fixed parameter before the variable parameter table. The traversal function must be called first.
(3) var_arg (AP, type): obtains the parameter value pointed to by the internal pointer of the current AP, moves the pointer to the next parameter, and specifies the type of the next parameter.
(4) va_end (AP): to traverse a variable parameter table, the AP and the parameter table are sorted as necessary. This function must be called at the end of traversal.
(5) va_copy (DEST, Src): introduced in c99. Copy SRC to DeST. Both DEST and SRC are va_list state variables. In this way, the second State Variable pointing to the current parameter is generated, and SRC and DEST can be used independently to traverse the variable parameter table. In DEST, you also need to call va_end like in SRC.
6. iso646.h:
Define some convenient macros for logical operators, which are added in addition to c89.

/* Iso c Standard: 7.9 some operator macros <iso646.h> */<br/> # ifndef _ iso646_h <br/> # DEFINE _ iso646_h <br/> # ifndef _ cplusplus <br/> # define and & <br/> # define and_eq & = <br/> # define bitand & <br/> # define bitor | <br/> # define compl ~ <Br/> # define not! <Br/> # define not_eq! = <Br/> # define or | <br/> # define or_eq | = <br/> # define XOR ^ <br/> # define xor_eq ^ = <br/> # endif <br/> # endif

The reason is &, | ,! , ^, Etc. These operators define a macro because it is inconvenient to use these special symbols in the ISO 646 character set, it is easier to use equivalent macro names and, bitor, not, and XOR. In C ++, these macro names are keywords. C89 addition 1 also provides characters that can be easily used in ISO 646 to spell symbols such. For example, <%, %>, <:,:>, %:, and %: % are equivalent to characters {,}, [,], #, and #.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.