#include
char getChar(int x,int y)
{
char c;
unsigned int a = x;
(a + y > 10) ? (c = 1) : (c = 2);
return c;
}
int main(void)
{
char c1 = getChar(7,4);
char c2 = getChar(7,3);
char c3 = getChar(7,-7);
char c4 = getChar(7,-8);
//c1 = 1
printf("c1 = %d\n", c1);
//c2 = 2
printf("c2 = %d\n", c2);
/*c3 = 2 无符号类型和有符号类型作运算,
* 有符号类型会隐示转换为无符号类型
* -7转换后的无符号值加上7刚好上溢出,结果是0
*/
printf("c3 = %d\n", c3);
/* c4 = 1
* -8转换后的无符号值加上7成为一个非常大的整数(差1就会溢出)
*/
printf("c4 = %d\n", c4);
return 0;
}
标题名称:【C语言面试题】第1题:有符号变量和无符号变量的值得转换
文章转载:
http://cdxtjz.cn/article/pipcdd.html