三目运算符这样用错哪儿了?

pinkfIoyd 2013-03-05 02:52:08
我以为和if-else是一样的!为什么会报错?

(temp1 != 0x0f)?(Length ++):(return Length);
...全文
361 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wizard_tiger 2013-03-06
  • 打赏
  • 举报
回复
ls都说了,三目运算符中不应有语句,应该是表达式。
赵4老师 2013-03-05
  • 打赏
  • 举报
回复
引用 12 楼 Mr_warm 的回复:
引用 11 楼 zhao4zhong2 的回复:不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 你咋带墨镜了?刚才还没带啊!
It's blind me.
pinkfIoyd 2013-03-05
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong2 的回复:
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!
你咋带墨镜了?刚才还没带啊!
zhao4zhong2 2013-03-05
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!
Saleayas 2013-03-05
  • 打赏
  • 举报
回复
return xxx 不是 表达式。
pinkfIoyd 2013-03-05
  • 打赏
  • 举报
回复
似乎明白了,现在主要问题如4楼和6楼所说的,但哪个对呢?还是这两个错误我都犯了?
rocktyt 2013-03-05
  • 打赏
  • 举报
回复
lz没理解语句和表达式的区别
赵4老师 2013-03-05
  • 打赏
  • 举报
回复
Expressions with the Conditional Operator The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows: The first operand is evaluated and all side effects are completed before continuing. If the first operand evaluates to true (a nonzero value), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated. The result of the conditional operator is the result of whichever operand is evaluated — the second or the third. Only one of the last two operands is evaluated in a conditional expression. Syntax conditional-expression : logical-or-expression logical-or-expression ? expression : conditional-expression Conditional expressions have no associativity. The first operand must be of integral or pointer type. The following rules apply to the second and third expressions: If both expressions are of the same type, the result is of that type. If both expressions are of arithmetic types, usual arithmetic conversions (covered in Arithmetic Conversions in Chapter 3) are performed to convert them to a common type. If both expressions are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type. If both expressions are of reference types, reference conversions are performed to convert them to a common type. If both expressions are of type void, the common type is type void. If both expressions are of a given class type, the common type is that class type. Any combinations of second and third operands not in the preceding list are illegal. The type of the result is the common type, and it is an l-value if both the second and third operands are of the same type and both are l-values. For example: (val >= 0) ? val : -val If the condition is true, the expression evaluates to val. If not, the expression equals –val
wushipin 2013-03-05
  • 打赏
  • 举报
回复
三目运算符是从右到左的结合方式,编译器编译的时候从右编译 遇到return 就当作一条完整的语句了,再检测三目运算的时候提示语法错误了
赵4老师 2013-03-05
  • 打赏
  • 举报
回复
return ((temp1 != 0x0f)?(Length ++):Length);
wfcdream 2013-03-05
  • 打赏
  • 举报
回复
return ...是语句,你见过那个运算符可以运算语句,运算符连接的应该是表达式。
pinkfIoyd 2013-03-05
  • 打赏
  • 举报
回复
为什么三目运算符中不能有return语句呢?还有其他例外吗?
pinkfIoyd 2013-03-05
  • 打赏
  • 举报
回复
引用 1 楼 liao05050075 的回复:
去掉那个return...
为什么要去掉它?return Length;也只是一条语句而已么?
liao05050075 2013-03-05
  • 打赏
  • 举报
回复
去掉那个return...

69,369

社区成员

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

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