gpt4 book ai didi

c - 字符表达式的大小

转载 作者:太空狗 更新时间:2023-10-29 16:42:45 24 4
gpt4 key购买 nike

有人能解释一下 sizeof(char expression) 是如何变成 sizeof(int) 的吗?

int main()
{
char a, b;
printf("%d\n", sizeof(a+b));
return 0;
}

程序打印 4 作为输出。我期望它是 sizeof(char)1

最佳答案

整数提升。

当小整数类型(小于int,如charshort等)在算术表达式中时,自动提升到 int


C11 §6.3.1.1 Boolean, characters, and integers

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned
int
. These are called the integer promotions. 58)

在脚注中:

58) The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.

关于c - 字符表达式的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25602002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com