8
#include <stdio.h>
 int main()        
{

           short int i = 20;

            char c = 97;

            printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i));
            return 0;
}

Could some one tell me what happens when sizeof(a+b) "a is short int type & b is char type" Output is : 2, 1, 4

Mani
  • 17,549
  • 13
  • 79
  • 100
hem
  • 85
  • 5

5 Answers5

8

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

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • And as you have already stated elsewhere, to avoid confusion, always put a whitespace between `sizeof` and the opening parenthesis. (I am just making this witty convention explicit for the reader) – Rerito May 07 '13 at 10:16
  • 1
    the code should be `printf("%zu, %zu, %zu\n", sizeof i, sizeof c, sizeof (c + i));` because [`sizeof` doesn't return an `int`](https://stackoverflow.com/q/940087/995714) – phuclv Dec 04 '17 at 03:08
  • @LưuVĩnhPhúc Absolutely, thanks for correcting this old answer. Edited, of course. – unwind Dec 04 '17 at 08:47
4
1) sizeof(i) ==> sizeof(short int) = 2

2) sizeof(c) ==> sizeof(char) = 1

3) sizeof(c + i [97+20]) ==> sizeof(int) = 4 // result in constant value which is int as default 
Mani
  • 17,549
  • 13
  • 79
  • 100
  • *"result in constant value which is int as default"* - Huh? I don't even understand what you're talking about. – Christian Rau May 07 '13 at 10:41
  • @ChristianRau I talk about `(c+i)` which result in constant value which is taken as `int` by default. – Mani May 07 '13 at 10:54
  • I don't why am I getting downvote again and again? For my `english` or Am I lead to wrong answer? – Mani May 07 '13 at 10:56
  • Since I cannot decipher the answer properly I am not sure if you're on the right track. But it doesn't really sound like you are refering to the integral promotion rules either (or at least don't explain it properly), so I have to assume the worst unfortunately. – Christian Rau May 07 '13 at 11:56
  • `c+i` resulting in a constant value (which isn't true anyway) has absolutely nothing to do with it. The actual explanation why it is `int` is because both operands are promoted to `int` before the addition and thus the sum has type `int`, too (look at some of the other answers). – Christian Rau May 07 '13 at 12:02
1

As others have told, sizeof is computed at compile-time.

Here, value of the expression c + i integer, as c and i are promoted (integral promotion) to int and thus

sizeof( c + i )

gives you 4 bytes on 32-bit machine..

VoidPointer
  • 3,037
  • 21
  • 25
0

sizeof only works at compiletime to get the size of an expression.

The following wont actually increase 'c':

c = sizeof(++c);

The expression sizeof(a + b) will return the largest type with unsigned having precedence

Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
0

The data type of a is "short int". -32768 ~ +32767

The data type of c is "char". 0 ~ 255

When you add a to c it is not either short int nor char, it becomes int!

Here is is an example code which help you to get the variable data type:

#include <typeinfo>

int main()
{
    short int a = 1;
    char c = 'A';
    std::cout << typeid(a + c).name() << std::endl;

    return 1;
}