为什么没有分号会给出错误,而太多的分号却不会? [英] Why no semicolon gives errors but too many of them don't?
问题描述
考虑这个 C 代码:
#include ;int main(void) {puts("你好,世界!");;;;返回0;;;;};
在这里,我几乎在所有可能的地方都放了分号.只是为了乐趣.但令人惊讶的是它奏效了!在包含之后我收到了关于分号的警告,但其他绝对错误的分号起作用了.如果我忘记在 puts
后面放一个分号,我会得到以下错误
错误:应为';'在'返回'之前
<小时>
为什么很多错误和无用的分号不会导致错误?在我看来,它们应该被视为语法错误.
单个分号构成一个空语句.它不仅合法,而且在某些情况下也很有用,例如,不需要实体的 while
/for
循环.一个例子:
while (*s++ = *t++);
<块引用>
C11 6.8.3 表达式和空语句
空语句(仅由分号组成)不执行任何操作.
<小时>
唯一的语法错误是这一行:
#include ;
Consider this C code:
#include <stdio.h>;
int main(void) {
puts("Hello, world!");; ;
;
return 0; ;
; ;
};
Here I've put semicolons almost everywhere possible. Just for fun. But surprisingly it worked! I got a warning about the semicolon after include but other absolutely wrong semicolons worked. If I forget to put a semicolon after puts
, I'll get the following error
error: expected ';' before 'return'
Why don't lots of wrong and useless semicolons cause errors? To my mind they should be treated as syntax errors.
A single semicolon constructs a null statement. It's not only legal, it's also useful in some cases, for instance, a while
/ for
loop that doesn't need a real body. An example:
while (*s++ = *t++)
;
C11 6.8.3 Expression and null statements
A null statement (consisting of just a semicolon) performs no operations.
The only syntax error is this line:
#include <stdio.h>;
这篇关于为什么没有分号会给出错误,而太多的分号却不会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!