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?