Why sizeof (! 5.6) gives output 2?

Even if we declare float a=5.6 , then printf("%d",sizeof(!a)) prints 2 . Why does it print the size of the integer?

+4
source share
4 answers

The operator ! returns an integer type, probably int . sizeof(int) == 2 according to your architecture, apparently.

+10
source

The operator ! does not return the type of operand. If you do NOT on a float , you will not get a float back. You will get an int with the logically opposite value of the initial float .

+3
source

According to fooobar.com/questions/235680 / ... , !E equivalent to 0==E and, as a result, it is an int type.

The result of the logical negation operator! equal to 0 if the value of its operand is not equal to 0, 1, if the value of its operand is compared to 0. The result is of type int. Expression! E is equivalent (0 == E).

sizeof(int) - 2 on your 16-bit architecture, explaining why sizeof(!a) outputs 2 to your computer.

+2
source

This can help.

 void main(){ int x = !4.3; printf("%d",x);//This will print 0 printf("%d",sizeof(0));//This will print 2 printf("%d",sizeof(!4.3));//Will print 2 } 

You will find that !4.3 will return 0. Therefore, sizeof(!4.3) = sizeof(0) = 2 (since 0 is an integer), so sizeof(!4.3) will be 2.

-1
source

Source: https://habr.com/ru/post/1488937/


All Articles