Search code examples
cbooleansizeofconditional-operatorcomma-operator

Why does sizeof(a ? true : false, a) operator printed one byte?


According to this question, sizeof(true) or sizeof(false) is 4 bytes because true and false are macros and defined in #include <stdbool.h> header file.

But, here interesting output of program.

#include <stdio.h>
#include <stdbool.h>

int main() 
{
    bool a = true;
    printf("%zu\n", sizeof(a ? true : false, a));
                             /* ^^ -> expresion2 */
    return 0;
}

Output(Live demo):

1

C11 6.5.15 Conditional operator(P4) :

The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.110)

Here, a is a bool type and assigned value true, then inside the sizeof operator, the conditional operator executed expression2 because a is a true.

So, comma operator part(expression3) not evaluated according to the standard and expression2 is a macro. So, according to that question, the output of macro is 4 bytes but here, the output of program is 1 byte.

So here, Why sizeof(a ? true : false, a) printed 1 byte only?


Solution

  • For starters consider the following simple demonstrative program

    #include <stdio.h>
    #include <stdbool.h>
    
    int main(void) 
    {
        printf( "sizeof( _Bool ) = %zu\n", sizeof( _Bool ) );
        printf( "sizeof( bool )  = %zu\n", sizeof( bool ) );
    
        return 0;
    }
    

    Its output is

    sizeof( _Bool ) = 1
    sizeof( bool ) = 1
    

    According to the C Standard (7.18 Boolean type and values )

    2 The macro

    bool
    

    expands to _Bool.

    Now let's consider the expression used in the sizeof operator in this call

    printf("%zu\n", sizeof(a ? true : false, a));
    

    According to the C Standard (6.5.15 Conditional operator)

    Syntax

    1 conditional-expression:
        logical-OR-expression
        logical-OR-expression ? expression : conditional-expression
    

    That is the conditional operator has higher precedence than the comma operator.

    So this expression

    a ? true : false, a
    

    is an expression with the comma operator and can be equivalently rewritten like

    ( a ? true : false ) , ( a )
    

    The result of the expression is the second operand of the comma operator that is it is the expression ( a ).

    As it was shown above this expression has the type _Bool because the variable a is declared like

    bool a = true;
    

    where the macro bool expands to _Bool. So sizeof( _Bool ) is equal to 1.

    If you will rewrite the conditional operator the following way as it is shown in this call

    printf( "%zu\n", sizeof( a ? true : a ) );
    

    then the output will be equal to the value returned by sizeof( int ) (usually equal to 4) because the type _Bool has less rank than the type int (the type of the constant integer literal of the expanded macro true) and as result the expression a will be implicitfly converted to the type int due to the integer promotions.