在C中,为什么sizeof(char) 1,而‘a’是int?

时间:2022-03-10 04:15:42

I tried

我试着

printf("%d, %d\n", sizeof(char), sizeof('c'));

printf(" % d % d \ n”,sizeof(char),sizeof(' c '));

and got 1, 4 as output. If size of a character is one, why does 'c' give me 4? I guess it's because it's an integer. So when I do char ch = 'c'; is there an implicit conversion happening, under the hood, from that 4 byte value to a 1 byte value when it's assigned to the char variable?

输出是1 4。如果一个字符的大小是1,为什么c给我4?我猜是因为它是一个整数。当我做char ch = 'c'时;是否存在隐式转换,在引擎盖下,从4字节的值到1字节的值,当它被分配给char变量时?

5 个解决方案

#1


35  

In C 'a' is an integer constant (!?!), so 4 is correct for your architecture. It is implicitly converted to char for the assignment. sizeof(char) is always 1 by definition. The standard doesn't say what units 1 is, but it is often bytes.

在C 'a中是一个整数常量(!?!),所以4对于您的体系结构来说是正确的。它隐式地转换为char作为赋值。根据定义,sizeof(char)总是1。标准没有说单位1是什么,但通常是字节。

#2


7  

Th C standard says that a character literal like 'a' is of type int, not type char. It therefore has (on your platform) sizeof == 4. See this question for a fuller discussion.

第三个标准说,像“a”这样的字符是int型的,而不是char型的。因此,它(在您的平台上)的sizeof = 4。请参阅这个问题以获得更全面的讨论。

#3


5  

It is the normal behavior of the sizeof operator (See Wikipedia):

这是sizeof运算符的正常行为(见*):

  • For a datatype, sizeof returns the size of the datatype. For char, you get 1.
  • 对于数据类型,sizeof返回数据类型的大小。对于char,得到1。
  • For an expression, sizeof returns the size of the type of the variable or expression. As a character literal is typed as int, you get 4.
  • 对于表达式,sizeof返回变量或表达式类型的大小。当字符文本被输入为int类型时,得到4。

#4


4  

This is covered in ISO C11 6.4.4.4 Character constants though it's largely unchanged from earlier standards. That states, in paragraph /10:

这在ISO C11 6.4.4.4中包含了字符常量,尽管它与以前的标准基本没有变化。在第/10段:

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer.

整数字符常量具有类型int.包含单个字符的整数字符常量的值映射到单字节执行字符,该值是被解释为整数的映射字符的表示的数值。

#5


0  

According to the ANSI C standards, a char gets promoted to an int in the context where integers are used, you used a integer format specifier in the printf hence the different values. A char is usually 1 byte but that is implementation defined based on the runtime and compiler.

根据ANSI C标准,在使用整数的上下文中,一个char被提升为一个int型,您在printf中使用了一个整数格式说明符,因此使用了不同的值。char通常是1字节,但这是基于运行时和编译器定义的实现。

#1


35  

In C 'a' is an integer constant (!?!), so 4 is correct for your architecture. It is implicitly converted to char for the assignment. sizeof(char) is always 1 by definition. The standard doesn't say what units 1 is, but it is often bytes.

在C 'a中是一个整数常量(!?!),所以4对于您的体系结构来说是正确的。它隐式地转换为char作为赋值。根据定义,sizeof(char)总是1。标准没有说单位1是什么,但通常是字节。

#2


7  

Th C standard says that a character literal like 'a' is of type int, not type char. It therefore has (on your platform) sizeof == 4. See this question for a fuller discussion.

第三个标准说,像“a”这样的字符是int型的,而不是char型的。因此,它(在您的平台上)的sizeof = 4。请参阅这个问题以获得更全面的讨论。

#3


5  

It is the normal behavior of the sizeof operator (See Wikipedia):

这是sizeof运算符的正常行为(见*):

  • For a datatype, sizeof returns the size of the datatype. For char, you get 1.
  • 对于数据类型,sizeof返回数据类型的大小。对于char,得到1。
  • For an expression, sizeof returns the size of the type of the variable or expression. As a character literal is typed as int, you get 4.
  • 对于表达式,sizeof返回变量或表达式类型的大小。当字符文本被输入为int类型时,得到4。

#4


4  

This is covered in ISO C11 6.4.4.4 Character constants though it's largely unchanged from earlier standards. That states, in paragraph /10:

这在ISO C11 6.4.4.4中包含了字符常量,尽管它与以前的标准基本没有变化。在第/10段:

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer.

整数字符常量具有类型int.包含单个字符的整数字符常量的值映射到单字节执行字符,该值是被解释为整数的映射字符的表示的数值。

#5


0  

According to the ANSI C standards, a char gets promoted to an int in the context where integers are used, you used a integer format specifier in the printf hence the different values. A char is usually 1 byte but that is implementation defined based on the runtime and compiler.

根据ANSI C标准,在使用整数的上下文中,一个char被提升为一个int型,您在printf中使用了一个整数格式说明符,因此使用了不同的值。char通常是1字节,但这是基于运行时和编译器定义的实现。