在 C++ 中,字符常量的大小是 char。在 C 中,字符常量的类型是整数 (int)。因此,在 C 中,32 位体系结构的 sizeof('a') 为 4,CHAR_BIT 为 8。但sizeof(char)对于 C 和 C++ 来说都是一个字节。
#include<stdio.h> main() { printf("%d", sizeof('a')); }输出结果
4
#include<iostream> using namespace std; main() { cout << sizeof('a'); }输出结果
1
在这两种情况下,我们都在做同样的事情。但在 C sizeof('a') 中返回 4,因为它被视为整数。但在 C++ 中它返回 1。它被视为字符。