admin管理员组文章数量:1432573
int i;
for (i = 0; i < 5; i++) {
printf(" %d \n", i);
}
output: 0 1 2 3 4
int i;
for (i = 0; i < 5; i++); {
printf(" %d \n", i);
}
output: 5
Why is the Semicolon make so different when put it after the for loop
int i;
for (i = 0; i < 5; i++) {
printf(" %d \n", i);
}
output: 0 1 2 3 4
int i;
for (i = 0; i < 5; i++); {
printf(" %d \n", i);
}
output: 5
Why is the Semicolon make so different when put it after the for loop
Share Improve this question asked Nov 18, 2024 at 17:29 Brando LuiBrando Lui 271 bronze badge 6 | Show 1 more comment5 Answers
Reset to default 4In your second code snippet you have a semicolon ;
after the for loop for (i = 0; i < 5; i++);
.
- The semicolon signifies the end of the statement and results in an empty body.
- In each interation
i++
, but body is empty due to;
, so nothing happens. i
becomes5
, causing loop to terminate.
Tip: Use an auto formatter. Saves times, makes for a better presentation too.
Using my Eclipse IDE
int i;
for (i = 0; i < 5; i++); {
printf(" %d \n", i);
}
Became:
int i;
for (i = 0; i < 5; i++)
;
{
printf(" %d \n", i);
}
And from there it is easier to see the printf()
is not part of the for()
loop. The loop simple incremented i
several times and then 5
was printed.
;
indicates the end of the statement. If the statement body is missing then it is called an empty statement. Statement can also contain many statements enclosed in {
}
for
loop structure:
for (initialization; condition; iteration) statement;
If you place the ;
just after the for
loop closing )
the statement will be empty. The compound statement after the semicolon is not part of the for
loop.
It is a very common source of bugs in C language. If you accidentally add a semicolon after the loop header when you intended to have a block of code, it can lead to unintended behaviour (as in your example):
for (i = 0; i < 5; i++);
{
printf(" %d \n", i);
}
The syntax is
for ( ... ) STMT
A curly-delimited block is a statement.
for ( ... ) { }
But so is an expression followed by a semi-colon.
for ( ... ) printf( ... );
And so is just a semi-colon.
for ( ... );
This is what you have. You're executing an empty statement 5 times, having no effect but changing i
.
The following is a cleaned-up version of your second snippet:
int i;
for (i = 0; i < 5; i++)
/* Do nothing */;
{
printf(" %d \n", i);
}
It's equivalent to this:
int i;
for (i = 0; i < 5; i++) {
/* Do nothing */
}
printf(" %d \n", i);
And it's thus equivalent to this:
int i = 5;
printf(" %d \n", i);
When you put a semicolon, it finishes the code, so when you put after parenthesis you finish the for loop and then print the last 'i', that is 5.
本文标签: C Syntax quotSemicolonquotStack Overflow
版权声明:本文标题:C Syntax "Semicolon" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745604834a2665778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
;
after thefor(...)
it means the body of the loop is empty. See: The Definitive C Book Guide and List. – wohlstad Commented Nov 18, 2024 at 17:34printf
call forms the body of thefor
loop. In the 2nd snippet, the body of thefor
loop is an empty statement terminated by the semicolon, and the compound statement containing theprintf
statement is a separate statement after thefor
loop statement. (The use of a compound statement in the 2nd snippet is redundant, but they can be useful for restricting the scope of declarations.) – Ian Abbott Commented Nov 18, 2024 at 18:17i
in the loop (as infor (int i = 0; i < 5; i++)
), the second example wouldn't compile because there wouldn't be a variablei
in scope unless your original code was hiding another variablei
(which would be bad practice). – Jonathan Leffler Commented Nov 18, 2024 at 20:05