-2
#include<stdio.h>

int main() {
 short int i=20;
char c=97;
printf("%d,%d,%d",sizeof(i),sizeof(c),sizeof(c+i));
return 0;
}

suppose given size of short int =2, char is 1 and of int is 4B Well If I'm running on machine it is giving 2,1,4 but ans is 2,1,2

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • What is the answer? Where did it come from? – Vlad from Moscow Dec 02 '17 at 10:11
  • The size of the integral types is machine dependent (excluding char whose size is defined as 1). It is possible for some types to have the same length; I would need to look that up in the standard but I think short, int and long could legally all have the same length. – Peter - Reinstate Monica Dec 02 '17 at 10:13
  • I think the question is "why is the result of short+char an int?" – DavidW Dec 02 '17 at 10:15
  • @DavidW Right -- I thought indeed that the operands are just promoted to the larger one of them, but they are promoted to int (if an int can hold the values). Vlad's answer is correct. I just looked it up in TCPL; while the signed/unsigned conversion rules have changed, this one is as old as the language. – Peter - Reinstate Monica Dec 02 '17 at 10:33

1 Answers1

5

For starters this call

printf("%d,%d,%d",sizeof(i),sizeof(c),sizeof(c+i));

has undefined behavior because there are used incorrect format specifiers.

It should look like

printf("%zu,%zu,%zu",sizeof(i),sizeof(c),sizeof(c+i));

because the type of the value evaluated by the sizeof operator is size_t.

Due to the integer promotions (as a part of the usual arithmetic conversions) the expression c + i has the type int.

From the C Standard (6.3.1 Arithmetic operands)

  1. ...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.58) All other types are unchanged by the integer promotions.

and (6.5.6 Additive operators)

4 If both operands have arithmetic type, the usual arithmetic conversions are performed on them.

Thus if the size of object of the type int is equal to 4 then the expression sizeof(c+i) yields 4.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank You , Sir :) – Pawan Kumar Dec 02 '17 at 10:39
  • I suppose that `%u` is on many platforms perfectly well defined for the result of `sizeof` as long as `size_t` is indeed an `unsigned int`. It's often a typedef, after all. The rationale for introducing `%z` and family has been to facilitate writing portable code. (The problem became of course more apparent after otherwise very similar PCs could have 64 or 32 bit architectures.) – Peter - Reinstate Monica Dec 02 '17 at 10:46
  • @PeterA.Schneider Usually size_t is a typedef name for unsigned long. So it would be better to specify %lu instead of %u. – Vlad from Moscow Dec 02 '17 at 10:48
  • I was just curious whether `%z` is really mandated (i.e. whether size_t is a fundamental type of its own). – Peter - Reinstate Monica Dec 02 '17 at 10:50