TL;DR answer:
sizeof result
is same as sizeof(char)
.
sizeof(num1+ num2)
is same as sizeof (int)
why?
In your case, they produce 1 (guaranteed by standard) and 4 (may vary), respectively.
That said, sizeof
produces a result of type size_t
, so you should %zu
format specifier to print the value.
Why:
First, for the addition operator +
, quoting C11
, chapter §6.5.6
If both operands have arithmetic type, the usual arithmetic conversions are performed on
them.
Regarding usual arithmetic conversions, §6.3.1.8/p1
[....] Otherwise, the integer promotions are performed on both operands.[...]
and then from §6.3.1.1,/p2,
If an int
can represent all values of the original type (as restricted by the width, for a
bit-field), the value is converted to an int
; otherwise, it is converted to an unsigned
int
. These are called the integer promotions.
So, sizeof(num1+num2)
is the same as sizeof(int)
.
sizeof(num1+num2)
is logically wrong. – i486 Jul 14 '16 at 7:57sizeof(num1)+sizeof(num2)
if you e.g. want to allocate space to serialize the data. – SF. Jul 14 '16 at 8:15sizeof
is used to get the size of data type likesizeof (int), sizeof (my_struct)
or size of specific variable/objectsizeof my_var, sizeof abc
. Maybe you want to writesizeof num1 + sizeof num2
? – i486 Jul 14 '16 at 8:58