From the C Standard (6.5.3.4 The sizeof and alignof operators)
2 The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is an integer constant.
And (6.5.3.3 Unary arithmetic operators)
5 The result of the logical negation operator ! is 0 if the value of
its operand compares unequal to 0, 1 if the value of its operand
compares equal to 0. The result has type int. The expression !E is
equivalent to (0==E).
Thus in this if statement
if(sizeof(!6.0))
the operand of the sizeof
operator is an expression with the negation operator !
and has the type int
. So in fact you have
if(sizeof(int))
sizeof( int )
is never equal to 0
so the expression in the if statement evaluates to logical true and the sub-statement of the if statement
printf("%d",sizeof(6.0,2));
gets the control.
The operand of this sizeof
operator sizeof(6.0,2)
is an expression with the comma operator.
From the C Standard (6.5.17 Comma operator)
2 The left operand of a comma operator is evaluated as a void
expression; there is a sequence point between its evaluation and that
of the right operand. Then the right operand is evaluated; the
result has its type and value.
So the type of the expression is int
that is the type of the second operand that represents the integer constant 2
.
So again the operator is equivalent to sizeof( int )
and in your system it is equal to 4
.
The compiler did not issue a warning for the expression with the comma operator that the evaluation of the first operand does not have an effect because the sizeof
operator does not evaluate expressions used as its operands. It only determines the type of expressions.
Pay attention to that you shell to use the conversion specifier %zu
instead of %d
in this call
printf("%zu",sizeof(6.0,2));