7

I'm confused about sizeof operator in C.

#include <stdio.h>

int main(void)
{
    char num1=1, num2=2, result;
    result = num1 + num2;
    printf("Size of result: %d \n",sizeof result);
    printf("Size of result: %d \n",sizeof(num1+num2));
}

The results are 1 and 4 respectively. Why does this happen?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Jin
  • 1,902
  • 3
  • 15
  • 26
  • `sizeof(num1+num2)` is logically wrong. – i486 Jul 14 '16 at 07:57
  • 3
    @i486 What do you mean? – Jin Jul 14 '16 at 08:00
  • @jwqwerty: Not logically wrong per se, but useless, as you're obtaining size of a temporary object that exists only for the duration of determining its size. In day-to-day usage, you'd be looking for `sizeof(num1)+sizeof(num2)` if you e.g. want to allocate space to serialize the data. – SF. Jul 14 '16 at 08:15
  • @jwqwerty `sizeof` is used to get the size of data type like `sizeof (int), sizeof (my_struct)` or size of specific variable/object `sizeof my_var, sizeof abc`. Maybe you want to write `sizeof num1 + sizeof num2`? – i486 Jul 14 '16 at 08:58

4 Answers4

5

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).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
4

result is of char type, therefore sizeof is giving 1 while num1+num2 promoted to int type and therefore it gives 4 (size of int).

Note that when an arithmetic operation is performed on a type smaller than that of int and all of it's value can be represented by int then result will be promoted to int type.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Why is num1+num2 promoted to int type? – Jin Jul 14 '16 at 07:48
  • @jwqwerty; Because of arithmetic operation. – haccks Jul 14 '16 at 07:50
  • @ haccks3 Then I guess in the line "result = num1 + num2", num1+num2 should also be promoted to int because it involves arithemetic operation? – Jin Jul 14 '16 at 07:52
  • @jwqwerty; It's the result of `num1 + num2` that is promoted to `int`. Assigning this result to a `char` type will not change the type of that variable. – haccks Jul 14 '16 at 07:55
  • @ haccks3 Oh I get it! Thanks!! – Jin Jul 14 '16 at 07:58
  • 1
    @jwqwerty: The result of the operation may not fit in the same type as the original, so type promotion is applied. Take `unsigned char a=200,b=200` - if you add them, you get 400, which won't fit. If you still insist on writing this to `unsigned char result=a+b;` result will be 144, because the variable gets truncated, data is lost because the container won't fit it. But during the addition, the 'temporary' container is expanded. – SF. Jul 14 '16 at 08:19
3

num1 + num2 is becoming integer and hence the output is 4 whereas result is char which outputs 1.

You can refer this article Integer Promotion:

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

the size of one char is 1byte, a char can hold values up to 127(unsigned up to 255). when you say something like (a + b) a temporary variable is created and used to add a to b, because a and b can hold only 127, they are promoted by the compiler to be an int, just to be sure.

which is logical because if a = 100 and b = 100, the user would like to see 200 when he adds them and not 73 (which is the result of an overflow).

monkeyStix
  • 620
  • 5
  • 10