Стандарт языка Си С99 TC (1113411), страница 12
Текст из файла (страница 12)
In particular, if == isdefined for type T, then x == y does not imply that memcmp(&x, &y, sizeof (T)) == 0.Furthermore, x == y does not necessarily imply that x and y have the same value; other operationson values of type T may distinguish between them.44) Some combinations of padding bits might generate trap representations, for example, if one paddingbit is a parity bit.
Regardless, no arithmetic operation on valid values can generate a traprepresentation other than as part of an exceptional condition such as an overflow, and this cannot occurwith unsigned types. All other combinations of padding bits are alternative object representations ofthe value specified by the value bits.38Language§6.2.6.2WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3there shall be exactly one sign bit. Each bit that is a value bit shall have the same value asthe same bit in the object representation of the corresponding unsigned type (if there areM value bits in the signed type and N in the unsigned type, then M ≤ N ).
If the sign bitis zero, it shall not affect the resulting value. If the sign bit is one, the value shall bemodified in one of the following ways:— the corresponding value with sign bit 0 is negated (sign and magnitude);— the sign bit has the value −(2 N ) (two’s complement);— the sign bit has the value −(2 N − 1) (ones’ complement ).Which of these applies is implementation-defined, as is whether the value with sign bit 1and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones’complement), is a trap representation or a normal value.
In the case of sign andmagnitude and ones’ complement, if this representation is a normal value it is called anegative zero.3If the implementation supports negative zeros, they shall be generated only by:— the &, |, ^, ~, <<, and >> operators with arguments that produce such a value;— the +, -, *, /, and % operators where one argument is a negative zero and the result iszero;— compound assignment operators based on the above cases.It is unspecified whether these cases actually generate a negative zero or a normal zero,and whether a negative zero becomes a normal zero when stored in an object.4If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<,and >> operators with arguments that would produce such a value is undefined.5The values of any padding bits are unspecified.45) A valid (non-trap) object representationof a signed integer type where the sign bit is zero is a valid object representation of thecorresponding unsigned type, and shall represent the same value.
For any integer type,the object representation where all the bits are zero shall be a representation of the valuezero in that type.6The precision of an integer type is the number of bits it uses to represent values,excluding any sign and padding bits. The width of an integer type is the same butincluding any sign bit; thus for unsigned integer types the two values are the same, while45) Some combinations of padding bits might generate trap representations, for example, if one paddingbit is a parity bit. Regardless, no arithmetic operation on valid values can generate a traprepresentation other than as part of an exceptional condition such as an overflow.
All othercombinations of padding bits are alternative object representations of the value specified by the valuebits.§6.2.6.2Language39ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256for signed integer types the width is one greater than the precision.6.2.7 Compatible type and composite type1Two types have compatible type if their types are the same. Additional rules fordetermining whether two types are compatible are described in 6.7.2 for type specifiers,in 6.7.3 for type qualifiers, and in 6.7.5 for declarators.46) Moreover, two structure,union, or enumerated types declared in separate translation units are compatible if theirtags and members satisfy the following requirements: If one is declared with a tag, theother shall be declared with the same tag.
If both are complete types, then the followingadditional requirements apply: there shall be a one-to-one correspondence between theirmembers such that each pair of corresponding members are declared with compatibletypes, and such that if one member of a corresponding pair is declared with a name, theother member is declared with the same name. For two structures, correspondingmembers shall be declared in the same order. For two structures or unions, correspondingbit-fields shall have the same widths. For two enumerations, corresponding membersshall have the same values.2All declarations that refer to the same object or function shall have compatible type;otherwise, the behavior is undefined.3A composite type can be constructed from two types that are compatible; it is a type thatis compatible with both of the two types and satisfies the following conditions:— If one type is an array of known constant size, the composite type is an array of thatsize; otherwise, if one type is a variable length array, the composite type is that type.— If only one type is a function type with a parameter type list (a function prototype),the composite type is a function prototype with the parameter type list.— If both types are function types with parameter type lists, the type of each parameterin the composite parameter type list is the composite type of the correspondingparameters.These rules apply recursively to the types from which the two types are derived.4For an identifier with internal or external linkage declared in a scope in which a priordeclaration of that identifier is visible,47) if the prior declaration specifies internal orexternal linkage, the type of the identifier at the later declaration becomes the compositetype.46) Two types need not be identical to be compatible.47) As specified in 6.2.1, the later declaration might hide the prior declaration.40Language§6.2.7WG14/N12565EXAMPLECommittee Draft — Septermber 7, 2007ISO/IEC 9899:TC3Given the following two file scope declarations:int f(int (*)(), double (*)[3]);int f(int (*)(char *), double (*)[]);The resulting composite type for the function is:int f(int (*)(char *), double (*)[3]);§6.2.7Language41ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N12566.3 Conversions1Several operators convert operand values from one type to another automatically.
Thissubclause specifies the result required from such an implicit conversion, as well as thosethat result from a cast operation (an explicit conversion). The list in 6.3.1.8 summarizesthe conversions performed by most ordinary operators; it is supplemented as required bythe discussion of each operator in 6.5.2Conversion of an operand value to a compatible type causes no change to the value or therepresentation.Forward references: cast operators (6.5.4).6.3.1 Arithmetic operands6.3.1.1 Boolean, characters, and integers1Every integer type has an integer conversion rank defined as follows:— No two signed integer types shall have the same rank, even if they have the samerepresentation.— The rank of a signed integer type shall be greater than the rank of any signed integertype with less precision.— The rank of long long int shall be greater than the rank of long int, whichshall be greater than the rank of int, which shall be greater than the rank of shortint, which shall be greater than the rank of signed char.— The rank of any unsigned integer type shall equal the rank of the correspondingsigned integer type, if any.— The rank of any standard integer type shall be greater than the rank of any extendedinteger type with the same width.— The rank of char shall equal the rank of signed char and unsigned char.— The rank of _Bool shall be less than the rank of all other standard integer types.— The rank of any enumerated type shall equal the rank of the compatible integer type(see 6.7.2.2).— The rank of any extended signed integer type relative to another extended signedinteger type with the same precision is implementation-defined, but still subject to theother rules for determining the integer conversion rank.— For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 hasgreater rank than T3, then T1 has greater rank than T3.2The following may be used in an expression wherever an int or unsigned int maybe used:42Language§6.3.1.1WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3— An object or expression with an integer type whose integer conversion rank is lessthan or equal to the rank of int and unsigned int.— A bit-field of type _Bool, int, signed int, or unsigned int.If an int can represent all values of the original type, the value is converted to an int;otherwise, it is converted to an unsigned int.
These are called the integerpromotions.48) All other types are unchanged by the integer promotions.3The integer promotions preserve value including sign. As discussed earlier, whether a‘‘plain’’ char is treated as signed is implementation-defined.Forward references: enumeration specifiers (6.7.2.2), structure and union specifiers(6.7.2.1).6.3.1.2 Boolean type1When any scalar value is converted to _Bool, the result is 0 if the value compares equalto 0; otherwise, the result is 1.6.3.1.3 Signed and unsigned integers1When a value with integer type is converted to another integer type other than _Bool, ifthe value can be represented by the new type, it is unchanged.2Otherwise, if the new type is unsigned, the value is converted by repeatedly adding orsubtracting one more than the maximum value that can be represented in the new typeuntil the value is in the range of the new type.49)3Otherwise, the new type is signed and the value cannot be represented in it; either theresult is implementation-defined or an implementation-defined signal is raised.6.3.1.4 Real floating and integer1When a finite value of real floating type is converted to an integer type other than _Bool,the fractional part is discarded (i.e., the value is truncated toward zero).