Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下代码:
int main() { char x='a'; printf("integer = %ld\ncharacter = %ld\nx = %ld\n", sizeof(int),sizeof('a'),sizeof(x); return 0; }
输出是:
整数 = 4 个 字符 = 4 x = 1
我的问题是,为什么char 变量 x的大小不等于'a'的大小。
实际上带来了什么不同?
谢谢您的帮助
因为在 C 中,单字符常量(例如,'a'或'0')的类型为intnot char。这在 C++ 中是不同的,它们的类型是char.
'a'
'0'
int
char