int main(void) {
printf("hello\n"), return 0;
}
•代码如上,,不能和return、break、continue连用,编译器会报错“expected expression”,这是语法上的限制吗?如果是,为什么要有这种限制呢?
好问题!我特意查了下 C Language References,找到了答案
原因在于: 逗号(comma)作为操作符(operator)只能连接表达式(expression),而 return 等不是表达式,而是语句(statement)。
先看官方定义:
comma operators: http://en.cppreference.com/w/c/language/operator_other
statements via http://en.cppreference.com/w/c/language/statements :
Statements are fragments of the C program that are executed in sequence. The body of any function is a compound statement, which, in turn is a sequence of statements and declarations
expressions via http://en.cppreference.com/w/c/language/expressions :
An expression is a sequence of operators and their operands, that specifies a computation.
表达式仅是一系列操作符及其操作数的有序组合,而语句则是一系列有固定结构的c片断,通常无返回值。
break, continue, return 都属于语句,因此不行。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。