code 1:
#include <stdio.h>
int main()
{
short int a = -1; // ----> Line 1
unsigned int b = 122; // ----> Line 2
if (a > b)
printf("a is big");
else
printf("a is small");
return 0;
}
Output: a is big
Since here the Line 1 is implicitly converted to Line 2, ie: signed short int to unsigned int.
similarly
code 2:
#include <stdio.h>
int main()
{
char a = -1; // ---> Line 3
unsigned short int b = 122; // ---> Line 4
if (a > b)
printf("a is big");
else
printf("a is small");
return 0;
}
Output: a is small
Here in code 2, since unsigned short int (Line 4) is greater than signed char (Line 3), why is the implicit conversion not happening?
or
Why doesn't the implicit conversion for unsigned short int and signed char take place?
char,signed char,unsigned char,short, andunsigned shortare never considered as result types of the integer promotions.The integer promotions originated in early C development where arithmetic was largely done using the native processor registers in their ordinary widths, and
charandshortwere types used for storing data. Theinttype was used as the “natural” type on the processor, matching its registers. Socharandshortdata was loaded into processor registers and then operated on with, in effect, instructions that performedintarithmetic. The current rules of standard C reflect this history; the integer promotions bring integer types up to at leastintorunsigned int.In your C implementation,
unsigned short intis narrower thanint, so it is promoted toint. So ischar. Thus thecharvalue −1 is promoted toint, yielding anintvalue of −1, and theunsigned short intvalue of 122 is promoted toint, yielding anintvalue of 122, and these are compared. Then −1 is not greater than 122, soa > bevaluates as false.The specific rules for the usual arithmetic conversions are:
If either operand (or, if complex, its corresponding real type) is
long double, the other operand is converted tolong double.Otherwise, if either operand (or, if complex, its corresponding real type) is
double, the other operand is converted todouble.Otherwise, if either operand (or, if complex, its corresponding real type) is
float, the other operand is converted tofloat.Otherwise, the integer promotions are performed on both operands: A value with a type with integer conversion rank less than
intis converted tointifintcan represent all values of the source type (for example, ifunsigned shortis the same width asint, it has some values thatintcannot represent) or tounsigned intotherwise. (Values with a type with greater rank are not converted.) Values with bit-field types of_Bool,intorunsigned intare also converted in this way.If both resulting operands have the same type, no further conversion is performed.
Otherwise, if both have signed type or both have unsigned type, the operand with lower rank is converted to the other type.
Otherwise, if rank of the unsigned type is at least the rank of the signed type, the signed type is converted to the unsigned type.
Otherwise, if the signed type can represent all the values of the unsigned type, the unsigned type is converted to the signed type.
Otherwise, both operands are converted to the unsigned type corresponding to the signed type. (For example, if the operands are
unsigned shortandint, and those have the same width, they will be converted tounsigned int.)