-3
    void main()
    {
     clrscr();
     if(sizeof(!6.0))
     printf("%d",sizeof(6.0,2));
     else
     printf("i don't know");
     getch();
    }

The output of this function is 4. As I can understand in the expression if(sizeof(!6.0)) -> the expression sizeof(!6.0) returns: 0

so sizeof(0) is 4 (in 64 bit system) and expression if(sizeof(!6.0)) executes as its True. But in printf("%d",sizeof(6.0,2)) the output is 4 .

So I want to know why sizeof() is returning 4 byte and how can it take two values without any warning and error by compiler.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    ```6.0,2``` returns the last element in the comma separated list (2). 2 is of type `int`. `int` is 4 bytes big in your architecture. – Raildex Aug 09 '21 at 11:06
  • 1
    You should really pay more attention to where your parentheses are – Mark Benningfield Aug 09 '21 at 11:07
  • I don't see any reason to dive deep into this material without any context. Nobody writes code like this. – Cheatah Aug 09 '21 at 11:08
  • gcc -Wall says "warning: left-hand operand of comma expression has no effect [-Wunused-value]". Pretty self-explanatory given that you know what the comma operator is. – Lundin Aug 09 '21 at 11:19

1 Answers1

2

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));
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335