Because of C's standard integral promotion rules, the type of the expression c + i
is int
, so that's why you're getting the equivalent of sizeof (int)
.
Note that sizeof
is not a function, the parenthesis are only needed when naming a type and to resolve precendence conflicts. Your code coule be written:
printf("%zu, %zu, %zu\n", sizeof i, sizeof c, sizeof (c + i));
The final use of sizeof
has parentheses since sizeof
binds tighter than +
, saying sizeof c + i
would be parsed as (sizeof c) + i
which is not the desired result.
Also note that it's a compile-time construct most of the time, the expression is never actually evaluated. All that happens is that the compiler "pretends" to evaluate it, to figure out the type of the expression, and then gives you the size of a value of that type. The actual value never needs to exist, which is sometimes neat.
The type of the value returned by sizeof
is size_t
, which is printed using %zu
(it's not int
).