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
  • i is set to 5 after the loop – 0stone0 Commented Nov 18, 2024 at 17:33
  • If you put ; after the for(...) it means the body of the loop is empty. See: The Definitive C Book Guide and List. – wohlstad Commented Nov 18, 2024 at 17:34
  • Because that's what syntax does? They are not, like, decorative semicolons. – wim Commented Nov 18, 2024 at 17:34
  • In the 1st snippet, the compound statement containing the printf call forms the body of the for loop. In the 2nd snippet, the body of the for loop is an empty statement terminated by the semicolon, and the compound statement containing the printf statement is a separate statement after the for 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:17
  • If you defined i in the loop (as in for (int i = 0; i < 5; i++)), the second example wouldn't compile because there wouldn't be a variable i in scope unless your original code was hiding another variable i (which would be bad practice). – Jonathan Leffler Commented Nov 18, 2024 at 20:05
 |  Show 1 more comment

5 Answers 5

Reset to default 4

In 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 becomes 5, 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