I have a small piece of code about the sizeof
operator with the ternary operator:
#include
#include
int main()
{
The ternary operator is a red herring.
printf("%zu\n", sizeof(true));
prints 4 (or whatever sizeof(int)
is on your platform).
The following assumes that bool
is a synonym for char
or a similar type of size 1, and int
is larger than char
.
The reason why sizeof(true) != sizeof(bool)
and sizeof(true) == sizeof(int)
is simply because true
is not an expression of type bool
. It's an expression of type int
. It is #define
d as 1
in stdbool.h
.
There are no rvalues of type Edit: this paragraph is not true, arguments to bool
in C at all. Every such rvalue is immediately promoted to int
, even when used as an argument to sizeof
.sizeof
don't get promoted to int
. This doesn't affect any of the conclusions though.