为什么sizeof('a')在C中是4?

Jee*_*tel 11 c sizeof char literals

可能重复:
为什么C字符文字的内部而不是字符?

#include<stdio.h>
int main(void)
{
    char b = 'c';
    printf("here size is %zu\n",sizeof('a'));
    printf("here size is %zu",sizeof(b));
}
Run Code Online (Sandbox Code Playgroud)

这里输出(见现场演示在这里.)

here size is 4 
here size is 1
Run Code Online (Sandbox Code Playgroud)

我不明白为什么sizeof('a')是4?

cni*_*tar 12

因为在C字符常量中,例如'a'具有类型int.

有一个关于这个项目的C FAQ:

也许令人惊讶的是,C中的字符常量是int类型,因此sizeof('a')是sizeof(int)(尽管这是C++不同的另一个区域).


San*_*raj 10

以下是著名的名句C书- The C programming Language通过Kernighan & Ritchie相对于单引号之间写入字符.

A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.

所以sizeof('a')相当于sizeof(int)