关于C中sizeof运算符的困惑

前端之家收集整理的这篇文章主要介绍了关于C中sizeof运算符的困惑前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对C中的sizeof运算符感到困惑.
#include <stdio.h>

int main(void)
{
    char num1=1,num2=2,result;
    result = num1 + num2;
    printf("Size of result: %d \n",sizeof result);
    printf("Size of result: %d \n",sizeof(num1+num2));
}

结果分别为1和4.为什么会这样?

@R_404_323@

结果是char类型,因此sizeof为1,而num1 num2提升为int类型,因此它给出4(int的大小).

请注意,当对小于int的类型执行算术运算时,所有值的值都可以用int表示,那么result将被提升为int类型.

原文链接:https://www.f2er.com/c/119276.html

猜你在找的C&C++相关文章