求解释 结果为1

Roy_Smiling 2012-08-16 08:05:22

int main()
{
unsigned int a = 6;
int b = -20;
char c;

(a+b > 6) ? (c = 1):(c = 0);

printf("%d\n",c);

system("pause");
return 1;
}
...全文
290 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangzhaoyang121 2012-08-17
  • 打赏
  • 举报
回复
所以一定要注意:当无符号和有符号比较,容易出错!!!
didijiji 2012-08-17
  • 打赏
  • 举报
回复
以前还真没注意到这个问题。
huangdancs 2012-08-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]
可是输出a+b就是-14,这里为什么不转换
[/Quote]

你是怎么输出a+b的值? %d?
FancyMouse 2012-08-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

可是输出a+b就是-14,这里为什么不转换
[/Quote]你用%d输出了,转换过去又被转换回来了
Roy_Smiling 2012-08-17
  • 打赏
  • 举报
回复
可是输出a+b就是-14,这里为什么不转换
happyhkhj 2012-08-17
  • 打赏
  • 举报
回复
“大”的往“小”的转
AXUBOD 2012-08-17
  • 打赏
  • 举报
回复
为什么一定是int转化为unsigned int啊??而不是unsigned int转化为int啊
EmberSpirit 2012-08-17
  • 打赏
  • 举报
回复
很明显是类型转换。
unsigned int型的数据和int 型的数据相加,int型数据会隐式转换为unsigned int,unsigned int型最高位不是符号位,它表示的全是正数,所以-20转换为一个很大的正数。
至于用printf("%d",a+b);输出显示-14,是因为%d是输出一个整型数据,它将a+b的类型又变为了int型,最高位又变成了符号位。所以显示的是负数。
楼主可以去看下不同数据类型间进行运算发生的隐式转换的详细情况。
wanglu343280746 2012-08-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
引用 12 楼 的回复:

可是输出a+b就是-14,这里为什么不转换
你用%d输出了,转换过去又被转换回来了
[/Quote]
++
aderan 2012-08-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

隐式类型转换。
[/Quote]

int在于unsigned long double进行数值运算时会转换

有符转无符,小字节转大字节。
byncz 2012-08-17
  • 打赏
  • 举报
回复
3楼正解!
mymtom 2012-08-17
  • 打赏
  • 举报
回复
ANSI C89
3.2.1.5 Usual arithmetic conversions

Many binary operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions: First, if either operand has type long double, the other operand is converted to long double. Otherwise, if either operand has type double, the other operand is converted to double. Otherwise, if either operand has type float, the other operand is converted to float. Otherwise, the integral promotions are performed on both operands. Then the following rules are applied: If either operand has type unsigned long int, the other operand is converted to unsigned long int. Otherwise, if one operand has type long int and the other has type unsigned int, if a long int can represent all values of an unsigned int, the operand of type unsigned int is converted to long int ; if a long int cannot represent all the values of an unsigned int, both operands are converted to unsigned long int. Otherwise, if either operand has type long int, the other operand is converted to long int. Otherwise, if either operand has type unsigned int, the other operand is converted to unsigned int. Otherwise, both operands have type int.

The values of operands and of the results of expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.
林荫客 2012-08-16
  • 打赏
  • 举报
回复
楼主努力,一起加油
hongwenjun 2012-08-16
  • 打赏
  • 举报
回复
#include <stdio.h>
int main()
{
unsigned int a = 6;
int b = -20;
char c;
((int)a + b > 6) ? (c = 1) : (c = 0); //搞清楚 为什么 (int)a 应该就知道问题答案了
printf("%d\n", c);
return 0;
}
tragedyhomeland 2012-08-16
  • 打赏
  • 举报
回复
是类型转换,有符号数按照无符号进行加法,肯定比0大很多。看来1楼是想证明什么,哎~~缺乏信心的孩子
天外淡云 2012-08-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
C/C++ code

int main()
{
unsigned int a = 6;
int b = -20;
char c;

(a+b > 6) ? (c = 1):(c = 0);

printf("%d\n",c);

system("pause");
return 1;
}
[/Quote]
这里的a和b相加,当然是要发生类型转换的,有符号类型服从无符号类型,结果肯定是为真,所以进行了赋值,c=1
Vincent_Song 2012-08-16
  • 打赏
  • 举报
回复
隐式类型转换。
smsgreenlife 2012-08-16
  • 打赏
  • 举报
回复
int main()
{
unsigned int a = 6;
int b = -20;
char c;
/*
* unsigned int 类型最大可表示2^32 - 1;int类型最大可表示2^31-1
* 所以,b 首先转为unsigned int,然后在做加法运算。
* b<0,也就是b的最高位符号位是1,转为unsigned int后原来的符号位不再表示正负。
* 因此b是一个很大的正数,a+b也就会很大(4294967282)
*/

(a+b > 6) ? (c = 1):(c = 0);

printf("%d\n",c);

system("pause");
return 1;
}

MFCANDPAI 2012-08-16
  • 打赏
  • 举报
回复
我不认这条帖子二,我知道进行了转换
huangdancs 2012-08-16
  • 打赏
  • 举报
回复

int main()
{
unsigned int a = 6;
int b = -20;
char c;
/*
* unsigned int 类型最大可表示2^32 - 1;int类型最大可表示2^31-1
* 所以,b 首先转为unsigned int,然后在做加法运算。
* b<0,也就是b的最高位符号位是1,转为unsigned int后原来的符号位不再表示正负。
* 因此b是一个很大的正数,a+b也就会很大(4294967282)
*/

(a+b > 6) ? (c = 1):(c = 0);

printf("%d\n",c);

system("pause");
return 1;
}
加载更多回复(1)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧