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

后端 未结 7 1399
爱一瞬间的悲伤
爱一瞬间的悲伤 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:20

    Here is a snippet from which is what included in the source

    #ifndef __cplusplus
    
    #define bool    _Bool
    #define true    1
    #define false   0
    
    #else /* __cplusplus */
    

    There macros true and false are declared as 1 and 0 respectively.

    however in this case the type is the type of the literal constants. Both 0 and 1 are integer constants that fit in an int, so their type is int.

    and the sizeof(int) in your case is 4.

提交回复
热议问题