Any guaranteed minimum sizes for types in C?
Can you generally make any assumptions about the minimum size of a data type?
What I have read so far:
float??? double???
Are the values in float.h
and limits.h
system dependent?
This is covered in the Wikipedia article:
A short int
must not be larger than an int
.
An int
must not be larger than a long int
.
A short int
must be at least 16 bits long.
An int
must be at least 16 bits long.
A long int
must be at least 32 bits long.
A long long int
must be at least 64 bits long.
The standard does not require that any of these sizes be necessarily different. It is perfectly valid, for example, if all four types are 64 bits long.
Yes, the values in float.h
and limits.h
are system dependent. You should never make assumptions about the width of a type, but the standard does lay down some minimums. See §6.2.5 and §5.2.4.2.1 in the C99 standard.
For example, the standard only says that a char
should be large enough to hold every character in the execution character set. It doesn't say how wide it is.
For the floating-point case, the standard hints at the order in which the widths of the types are given:
§6.2.5.10
There are three real floating types, designated as float , double , and long double . 32) The set of values of the type float is a subset of the set of values of the type double ; the set of values of the type double is a subset of the set of values of the type long double .
They implicitly defined which is wider than the other, but not specifically how wide they are. "Subset" itself is vague, because a long double
can have the exact same range of a double
and satisfy this clause.
This is pretty typical of how C goes, and a lot is left to each individual environment. You can't assume, you have to ask the compiler.
However, the new C99 specifies (in stdint.h
) optional types of minimal sizes, like uint_least8_t
, int_least32_t
, and so on..
(see en_wikipedia_Stdint_h)
上一篇: 如何打印pthread
下一篇: 任何C类型的保证最小尺寸?