sizeof(unsigned char)也保证为1吗?

时间:2014-12-13 23:46:27

标签: c++ sizeof

我认为标题是不言自明的,但更好的格式。

sizeof(unsigned char)也保证是1吗?


我不相信这个问题是重复的,因为:

  • C与C ++(标签摘录:...It was originally designed as an extension to C, and keeps a similar syntax, but is now a completely different language...)不同。
  • char可以是已签名或未签名。没有理由假设sizeof(unsigned char) == sizeof(char),除非你得到标准的保证,因此问题。
  • 即使您将C与C ++混淆,答案也不会重叠。

2 个答案:

答案 0 :(得分:8)

参考:http://en.cppreference.com/w/cpp/language/sizeof

  

sizeof(char),sizeof(signed char)和sizeof(unsigned char)总是如此   返回1.

答案 1 :(得分:6)

[expr.sizeof] / 1:

  

sizeof(char)sizeof(signed char)sizeof(unsigned char)1

另外,[basic.fundamental] / 1:

  

charsigned charunsigned char占用相同数量的存储空间并具有相同的对齐要求