Quantcast
Channel: Implicit type promotion rules - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Implicit type promotion rules

$
0
0

This post is meant to be used as a FAQ regarding implicit integer promotion in C, particularly implicit promotion caused by the usual arithmetic conversions and/or the integer promotions.

Example 1)
Why does this give a strange, large integer number and not 255?

unsigned char x = 0;unsigned char y = 1;printf("%u\n", x - y); 

Example 2)
Why does this give "-1 is larger than 0"?

unsigned int a = 1;signed int b = -2;if(a + b > 0)  puts("-1 is larger than 0");

Example 3)
Why does changing the type in the above example to short fix the problem?

unsigned short a = 1;signed short b = -2;if(a + b > 0)  puts("-1 is larger than 0"); // will not print

(These examples were intended for a 32 or 64 bit computer with 16 bit short.)


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles



Latest Images