1

I know the range of short is from 32767 to -32768 on most systems. But when I try to assign 32787 I see something different.

#include <stdio.h>
int main ()
{
   short int a;
   a = 32787;
   printf("%hi\n",a);
}

Output:

-32749

This value actually my value + minimum short value + minimum short value and not some random value. (32787 + (-32768)) + (-32768) = -32749

This is the first case I try to assign a high value to short int I will get warning from GCC I get some value.

This makes the results completely incomprehensible. Does anyone know what's going on here?

user16217248
  • 3,119
  • 19
  • 19
  • 37
Pekenbower
  • 11
  • 3
  • 1
    `Which makes the results completely incomprehensible. Does anyone know what's going on here?` - what do you mean by this? You have more test cases? – wkl Sep 04 '22 at 17:43
  • Can you explain more clearly what you did in your second case, that made the results "completely incomprehensible"? This is technically implementation defined behavior (prior to C2x) but you should normally observe behavior consistent with your first case. – zwol Sep 04 '22 at 17:43

0 Answers0