c++ - 为什么 C 字 rune 字是 int 而不是 char?

标签 c++ c char sizeof

在 C++ 中,sizeof('a') == sizeof(char) == 1。这很直观,因为 'a' 是字 rune 字,并且 sizeof(char) == 1 由标准定义。

但是在 C 语言中,sizeof('a') == sizeof(int)。也就是说,看起来 C 字 rune 字实际上是整数。有谁知道为什么?我可以找到很多关于这个 C 怪癖的提及,但没有解释它为什么存在。

最佳答案

关于same subject的讨论

"More specifically the integral promotions. In K&R C it was virtually (?) impossible to use a character value without it being promoted to int first, so making character constant int in the first place eliminated that step. There were and still are multi character constants such as 'abcd' or however many will fit in an int."

关于c++ - 为什么 C 字 rune 字是 int 而不是 char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56536401/

相关文章:

c++ - 数组/指针混淆

c++ - 为什么范围解析不适用于重写变量?

c - 为什么 sbrk() 不告诉我正在使用多少堆?

c++ - 从字符串中删除重复的字符

c++ - 如何声明已在类中声明的 char 变量的 Char 数组?

c++ - 使用文件中的数据在对话框中显示多个图像 (C++ MFC)

c++ - C代码的内存要求

C:如何在二维字符数组的每个空间中放置一个字符串?

c - c - 如何在文件中写入两个字符串,用空格或逗号分隔?

c++ - C++错误: expected ',' or ';' before '{' token when their is