C语言 “for”循环后分号的影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13421395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Effect of semicolon after 'for' loop
提问by user1830323
Say I want to print a message in C five times using a forloop. Why is it that if I add a semicolon after for loop like this:
假设我想使用for循环在 C 中打印一条消息五次。为什么我在 for 循环后添加一个分号,如下所示:
for (i=0;i<5;i++);
the message does not get printed 5 times, but it does if I do not put the semicolon there?
该消息不会打印 5 次,但如果我不将分号放在那里,它会打印吗?
回答by dasblinkenlight
Semicolon is a legitimate statement called null statement*that means "do nothing". Since the forloop executes a single operation (which could be a block enclosed in {}) semicolon is treated as the body of the loop, resulting in the behavior that you observed.
分号是一个合法的语句,称为空语句*,意思是“什么都不做”。由于for循环执行单个操作(可能是包含在 中的块{})分号被视为循环的主体,从而导致您观察到的行为。
The following code
以下代码
for (i=0;i<5;i++);
{
printf("hello\n");
}
is interpreted as follows:
解释如下:
- Repeat five times
for (i=0;i<5;i++) - ... do nothing (semicolon)
- Open a new scope for local variables
{ - ... Print "hello"
- Close the scope
}
- 重复五次
for (i=0;i<5;i++) - ...什么都不做(分号)
- 为局部变量打开一个新的作用域
{ - ...打印“你好”
- 关闭范围
}
As you can see, the operation that gets repeated is ;, not the printf.
如您所见,重复的操作是;,而不是printf。
**见K&RK&R,第 1.5.2 节
回答by Prince John Wesley
for (i=0;i<5;i++);
is equivalent to
相当于
for (i=0;i<5;i++){}
回答by ouah
The statement consisting of just the ;token is called the null statementand it does just... nothing.
仅由;标记组成的语句称为空语句,它只是……什么也不做。
For example, this is valid:
例如,这是有效的:
void foo(void)
{
;
;
;
}
It can be used everywhere a statement can be used, for example in:
它可以用于任何可以使用语句的地方,例如:
if (bla)
;
else
;
See the C Standard paragraph:
请参阅 C 标准段落:
(C99, 6.8.3p3) "A null statement (consisting of just a semicolon) performs no operations."
(C99, 6.8.3p3) “空语句(仅由分号组成)不执行任何操作。”
回答by Sunil B N
This code below will print "Hello" 5 times..
下面的代码将打印“Hello” 5 次..
for(i=0;i<5,printf("Hello\n");i++);
回答by Rahul Vashisth
Many compilers show a syntax error when you put a semicolon after a for loop but according to gcc compiler(Linux) or Dev-cpp you can put a semicolon after a for loop and it will not show you any errors.
当您在 for 循环后放置分号时,许多编译器会显示语法错误,但根据 gcc 编译器(Linux)或 Dev-cpp,您可以在 for 循环后放置分号,并且不会显示任何错误。
For example
例如
for(int i=0;i<=5;i++);
or
或者
for(int i=0;i<=5;i++)
{//blank body}
From the above example it is clear if we put blank braces or semicolon after for loop that means we haven't entered any variable yet.
从上面的例子可以清楚地看出,如果我们在 for 循环后放置空大括号或分号,这意味着我们还没有输入任何变量。
Now come to your question.
If you want to print hello five times, you have to write your program as
现在来回答你的问题。
如果你想打印五次你好,你必须把你的程序写成
for(int i=0;i<5;i++)
{
printf("hello");
}
I hope you understand
cheers!!
Rahul Vashisth
我希望你理解
欢呼!!
拉胡尔·瓦西斯

