Why does sizeof(!0) print 1 instead of 4?

  • A+
#include <iostream>  int main() {     std::cout<<sizeof(0);     return 0; } 

Here, sizeof(0) is 4 in C++ because 0 is an integer rvalue.

But, If I write like this:


here, sizeof(!0) is 1. But, !0 means it print 1, which is also, int type.

then, Why does sizeof(!0) print 1 instead of 4? What am I miss here?


The logical negation operator:

! rhs 

If the operand is not bool, it is converted to bool using contextual conversion to bool: it is only well-formed if the declaration bool t(arg) is well-formed, for some invented temporary t.

The result is a bool prvalue.

And sizeof bool which is implementation defined is 1 in your implementation.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: