Why does “sizeof(a ? true : false)” give an output of four bytes?

后端 未结 7 1403
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 07:41

I have a small piece of code about the sizeof operator with the ternary operator:

#include 
#include 

int main()
{
         


        
7条回答
  •  孤独总比滥情好
    2021-01-30 08:30

    It's because you have #include . That header defines macros true and false to be 1 and 0, so your statement looks like this:

    printf("%zu\n", sizeof(a ? 1 : 0)); // Why 4?
    

    sizeof(int) is 4 on your platform.

提交回复
热议问题