gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:16:10 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com