在以下代码中:
#include "stdio.h"
signed char a= 0x80;
unsigned char b= 0x01;
void main (void)
{
if(b*a>1)
printf("promoted\n");
else if (b*a<1)
printf("why doesnt promotion work?");
while(1);
}
我原本期望会输出"promoted",但实际上没有。为什么呢?如果我将数据类型更改为有符号和无符号整数,并将a设置为负数,例如0x80000000,将b设置为正数,例如0x01,则会按预期输出"promoted"。
请帮助我理解问题所在!
signed char
和unsigned char
都比int
低级,因此它们都会被提升为int
。在signed int
vs.unsigned int
的情况下,标准规定它们都将转换为unsigned int
。有关规则可以在此处找到:https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules - Mysticial