免费注册 查看新帖 |

Chinaunix

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

C预处理条件判断语句中的操作符优先级 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-11 15:44 |只看该作者 |倒序浏览
30可用积分
1. 预处理中的操作符优先级和C语言的相同吗?
2. 否定操作符 ! 的优先级 在什么之前,在什么之后?
3. 三目操作符,在预处理中有效吗?

最佳答案

查看完整内容

1.预处理对constant-expression求值,并没有用到特殊的语法规则。和非预处理的constant-exprssion相同。2.显式转换之前,后缀操作符之后。3.有效,条件表达式可以是constant-expression。当然有点限制,需要保证能在预处理时求值。依据:ISO C116.6 Constant expressionsSyntax1 constant-expression:conditional-expressionDescription2 A constant expression can be evaluated during translation rather than runtime, and acc ...

论坛徽章:
0
2 [报告]
发表于 2012-01-11 15:44 |只看该作者
本帖最后由 幻の上帝 于 2012-01-12 13:08 编辑

1.预处理对constant-expression求值,并没有用到特殊的语法规则。和非预处理的constant-exprssion相同。
2.显式转换之前,后缀操作符之后。
3.有效,条件表达式可以是constant-expression。当然有点限制,需要保证能在预处理时求值。
依据:
ISO C11
6.6 Constant expressions
Syntax
1 constant-expression:
conditional-expression
Description
2 A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.
Constraints
3 Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.115)
4 Each constant expression shall evaluate to a constant that is in the range of representable values for its type.
115) The operand of a sizeof operator is usually not evaluated (6.5.3.4).

Annex A.2.1
(6.5.2) postfix-expression:
primary-expression
postfix-expression [ expression ]
postfix-expression ( argument-expression-listopt )
postfix-expression . identifier
postfix-expression -> identifier
postfix-expression ++
postfix-expression --
( type-name ) { initializer-list }
( type-name ) { initializer-list , }

(6.5.2) argument-expression-list:
assignment-expression
argument-expression-list , assignment-expression

(6.5.3) unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )
alignof ( type-name )

(6.5.3) unary-operator: one of
& * + - ~ !

(6.5.4) cast-expression:
unary-expression
( type-name ) cast-expression


(6.5.15) conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

(6.6) constant-expression:
conditional-expression

(6.10)
if-group:
# if constant-expression new-line groupopt
# ifdef identifier new-line groupopt
# ifndef identifier new-line groupopt

elif-group:
# elif constant-expression new-line groupopt

论坛徽章:
0
3 [报告]
发表于 2012-01-12 10:59 |只看该作者
这些问题最好就是查 标准 文件
人家的回答你敢全信么?即使是编译器测试过,那代表其他编译器也能通过么?

所以,一份 c99 标准文件还是需要的……

论坛徽章:
0
4 [报告]
发表于 2012-01-12 12:42 |只看该作者
加上括号不就行了。

论坛徽章:
0
5 [报告]
发表于 2012-01-12 13:57 |只看该作者
回复 4# 幻の上帝


    求传送门

论坛徽章:
0
6 [报告]
发表于 2012-01-12 15:18 |只看该作者
虽然看不太懂,但还是感谢这么细致的回答。另外这个文档哪里有?

论坛徽章:
0
7 [报告]
发表于 2012-01-14 15:49 |只看该作者
本帖最后由 幻の上帝 于 2012-01-14 15:55 编辑

回复 5# hbmhalley
回复 6# Perlvim


http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP