c++ - unsigned关键字会影响sizeof的结果吗?

标签 c++ c sizeof unsigned

C 和 C++ 是否保证类型的无符号等价物具有相同的大小?

例子:

size_t size = sizeof(unsigned int);

unsigned 在这里完全没有实际意义吗?

最佳答案

两种语言都保证相应标准整数类型的有符号和无符号变体具有相同的大小。

C++,委员会草案 n3337,3.9.1/3:

3 For each of the standard signed integer types, there exists a corresponding (but different) standard un- signed integer type: “unsigned char”, “unsigned short int”, “unsigned int”, “unsigned long int”, and “unsigned long long int”, each of which occupies the same amount of storage and has the same alignment requirements (3.11) as the corresponding signed integer type45; that is, each signed integer type has the same object representation as its corresponding unsigned integer type. [...]

对于C来说,写法很相似

摘自 n1570 草案,6.2.5/6:

For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements. The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types. The unsigned integer types that correspond to the extended signed integer types are the extended unsigned integer types. The standard and extended unsigned integer types are collectively called unsigned integer types.

关于c++ - unsigned关键字会影响sizeof的结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16616299/

相关文章:

c++ - 奇数单元格的平均值?

c - 如何使用 strtol 读取 __int64 值?

c - 作者插入不需要的换行符

c - 如何从 .t​​xt C 中的数组中删除项目

c++ - sizeof(some pointer) 是否总是等于四?

c++ - VIM 中 c++ 类标签的确定性导航。 ctags 效果不佳

android - 适用于 android 的 native c++ 3d 数学/几何库?

c++ - 成员(member)模板功能。为什么不编译?

c - 链表中的节点是单独的结构还是同一结构的一部分?

c - 结构是巨大的。为什么?