对于积分/运算类型C和C尺寸保证++ [英] size guarantee for integral/arithmetic types in C and C++
问题描述
我知道C ++标准明确保障的只有字符
,符号字符
和<$ C $大小C> unsigned char型。此外,它提供了保证,比如,短
至少是一样大的字符
, INT
大如短
等,但有关,绝对值没有明确保证说,的sizeof(INT)
。这是在我脑海中的信息,我与它幸福地生活在一起。前一段时间,但是,我碰到一个注释进来SO(无法找到它),在C 长
是保证至少有4个字节,即要求由C ++继承。是这样吗?如果是这样,我们还有什么其他的隐性担保算术类型的C ++的大小?请注意,我绝对不感兴趣的实际的跨越在这个问题上不同的平台保障,只是理论的人。
I know that the C++ standard explicitly guarantees the size of only char
, signed char
and unsigned char
. Also it gives guarantees that, say, short
is at least as big as char
, int
as big as short
etc. But no explicit guarantees about absolute value of, say, sizeof(int)
. This was the info in my head and I lived happily with it. Some time ago, however, I came across a comment in SO (can't find it) that in C long
is guaranteed to be at least 4 bytes, and that requirement is "inherited" by C++. Is that the case? If so, what other implicit guarantees do we have for the sizes of arithmetic types in C++? Please note that I am absolutely not interested in practical guarantees across different platforms in this question, just theoretical ones.
推荐答案
18.2.2保证&LT; climits&GT;
具有相同的内容的C库头&LT; limits.h中方式&gt;
18.2.2 guarantees that <climits>
has the same contents as the C library header <limits.h>
.
在ISO C90标准是棘手弄个,这是考虑到C ++依赖于一种耻辱,但部分数字限制(随机草案一追查有一次编号为2.2.4.2,并撒谎左右)给出了最小值为 INT_MAX
等在&LT常数; limits.h中&GT;
。例如 ULONG_MAX
必须至少4294967295,从中我们推断的宽度长
至少是32位。
The ISO C90 standard is tricky to get hold of, which is a shame considering that C++ relies on it, but the section "Numerical limits" (numbered 2.2.4.2 in a random draft I tracked down on one occasion and have lying around) gives minimum values for the INT_MAX
etc. constants in <limits.h>
. For example ULONG_MAX
must be at least 4294967295, from which we deduce that the width of long
is at least 32 bits.
有在C99标准类似的限制,当然这些都不是用C ++ 03中引用的那些。
There are similar restrictions in the C99 standard, but of course those aren't the ones referenced by C++03.
这确实的不的保证长
至少是4个字节,因为在C和C ++字节基本上是定义为CHAR ,并且不保证 CHAR_BIT
是用C或C ++ 8。 CHAR_BIT == 8
是由两个POSIX和Windows保障。
This does not guarantee that long
is at least 4 bytes, since in C and C++ "byte" is basically defined to mean "char", and it is not guaranteed that CHAR_BIT
is 8 in C or C++. CHAR_BIT == 8
is guaranteed by both POSIX and Windows.
这篇关于对于积分/运算类型C和C尺寸保证++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!