免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1230 | 回复: 3
打印 上一主题 下一主题

条件运算,表达式最终的值的类型是? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-01 19:31 |只看该作者 |倒序浏览
表达式1?表达式2:表达式3
这是条件表达式的形式,其中1,2,3式的类型可以不相同,那么最后整个表达式的值的类型是三个表达式中最高型呢?还是不考虑最高型问题,直接取2式或3式的值.
请给个有依据的准确说法.能举出例子更好.

论坛徽章:
0
2 [报告]
发表于 2005-10-01 19:37 |只看该作者

条件运算,表达式最终的值的类型是?

试了一下,按默认规则cast为最高型别。

          char b=1;
          int a=1;
          double c=1.0;
      
          printf("%d\n",sizeof(b?a:c));

其中的值换来换去看结果就有答案了


p.s.:为什么不自己实验呢?我也没研究过,写几行不就知道答案了?

论坛徽章:
0
3 [报告]
发表于 2005-10-01 20:27 |只看该作者

条件运算,表达式最终的值的类型是?

原帖由 "小水平" 发表:

请给个有依据的准确说法.


“有依据的准确说法”如下
c99 “6.5.15 Conditional operator”

The first operand is evaluated; there is a sequence point after its evaluation. The second
operand is evaluated only if the first compares unequal to 0; the third operand is evaluated
only if the first compares equal to 0; the result is the value of the second or third operand
(whichever is evaluated), converted to the type described below.92) If an attempt is made
to modify the result of a conditional operator or to access it after the next sequence point,
the behavior is undefined.

If both the second and third operands have arithmetic type, the result type that would be
determined by the usual arithmetic conversions, were they applied to those two operands,
is the type of the result. If both the operands have structure or union type, the result has
that type. If both operands have void type, the result has void type.

If both the second and third operands are pointers or one is a null pointer constant and the
other is a pointer, the result type is a pointer to a type qualified with all the type qualifiers
of the types pointed-to by both operands. Furthermore, if both operands are pointers to
compatible types or to differently qualified versions of compatible types, the result type is
a pointer to an appropriately qualified version of the composite type; if one operand is a
null pointer constant, the result has the type of the other operand; otherwise, one operand
is a pointer to void or a qualified version of void, in which case the result type is a pointer to
an appropriately qualified version of void.

论坛徽章:
0
4 [报告]
发表于 2005-10-01 21:38 |只看该作者

条件运算,表达式最终的值的类型是?

原帖由 "zalem" 发表:
试了一下,按默认规则cast为最高型别。

          char b=1;
          int a=1;
          double c=1.0;
      
          printf("%d\n",sizeof(b?a:c));

其中的值换来换去看结果就有答案了


p.s.:..........

真是太棒了,聪明,我不是懒,而是没想到这么好的用法.简单,明了,还说明问题.太感谢了,佩服.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP