Why does sizeof(char + char) return 4?

后端 未结 2 1598
误落风尘
误落风尘 2021-01-17 18:30
char  a, b;     
printf(\"%d\", sizeof(a+b));

What will printf write to the screen?

I thought because sizeof(char)=1, that sizeof(a+b) wil

2条回答
  •  天命终不由人
    2021-01-17 19:07

    This is not the same as sizeof(char), the argument (i.e. the result of the addition) is promoted to int so sizeof(a + b) is in fact equivalent to sizeof(int). If you cast the result to char it will be what you expect. Also, the correct format specifier for sizeof result which is size_t is %zu and not %d.

    Try

    printf("%zu", sizeof((char) (a + b)));
    

提交回复
热议问题