main() {
unsigned x = 1;
char y = -1;
if (x > y)
printf("x>y");
else
printf("x<=y");
}
I expected x>y,
but I had to change unsigned int to signed int to get the expected result.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If
charis equivalent tosigned char:charis promoted toint(Integer Promotions, ISO C99 §6.3.1.1 ¶2)intandunsigned inthave the same rank,intis converted tounsigned int(Arithmetic Conversions, ISO C99 §6.3.1.8)If
charis equivalent tounsigned char:charmay be promoted to eitherintorunsigned int:intcan represent allunsigned charvalues (typically becausesizeof(int) > sizeof(char)),charis converted toint.sizeof(char)==sizeof(int)),charis converted tounsigned.intorunsigned int, and another that isunsigned int. The first operand is converted tounsigned int.Integer promotions:
An expression of a type of lower rank that
intis converted tointifintcan hold all of the values of the original type, tounsigned intotherwise.Arithmetic conversions:
Try to convert to the larger type. When there is conflict between signed and unsigned, if the larger (including the case where the two types have the same rank) type is unsigned, go with unsigned. Otherwise, go with signed only in the case it can represent all the values of both types.
Conversions to integer types(ISO C99 §6.3.1.3):
Conversion of an out-of-range value to an unsigned integer type is done via wrap-around (modular arithmetic).
Conversion of an out-of-range value to a signed integer type is implementation defined, and can raise a signal (such as SIGFPE).