Effect of semicolon after 'for' loop

61,358

Solution 1

Semicolon is a legitimate statement called null statement * that means "do nothing". Since the for loop 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.

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 }

As you can see, the operation that gets repeated is ;, not the printf.


* See K&R, section 1.5.2

Solution 2

for (i=0;i<5;i++);

is equivalent to

for (i=0;i<5;i++){}

Solution 3

The statement consisting of just the ; token is called the null statement and 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:

(C99, 6.8.3p3) "A null statement (consisting of just a semicolon) performs no operations."

Solution 4

This code below will print "Hello" 5 times..

    for(i=0;i<5,printf("Hello\n");i++);

Solution 5

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 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.

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

Share:
61,358
user1830323
Author by

user1830323

Updated on July 05, 2022

Comments

  • user1830323
    user1830323 almost 2 years

    Say I want to print a message in C five times using a for loop. Why is it that if I add a semicolon after for loop like this:

    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?

  • ams
    ams over 11 years
    Semicolon isn't an "operator". It terminates a "statement", and it's valid for the statement to contain nothing. Some compilers warn about this because it is a common mistake. Empty braces {} is the preferred way to do nothing.
  • ouah
    ouah over 11 years
    Semicolon is a punctuator but not an operator.
  • Sergey Kalinichenko
    Sergey Kalinichenko over 11 years
    @ams Please see an edit. I was wrong in calling it an operator, it is a statement (at least according to K&R)
  • Sergey Kalinichenko
    Sergey Kalinichenko over 11 years
    @ouah Please see an edit. I was wrong in calling it an operator, it is a statement (at least according to K&R)
  • Jonathan Leffler
    Jonathan Leffler over 9 years
    Actually, no that won't print Hello 5 times; it will print Hello an indefinitely large number of times. This is because the condition is i < 5, printf("Hello\n"); and the comma operator evaluates its LHS (i < 5) and ignores the result, then evaluates the RHS (printf("Hello\n")), which returns 6 because it writes 6 characters to standard output; because 6 is true (it isn't zero), the loop continues, indefinitely. With my default compiler options for GCC (including -Werror), the code doesn't compile: error: left-hand operand of comma expression has no effect [-Werror=unused-value].
  • Sapphire_Brick
    Sapphire_Brick over 3 years
    @SergeyKalinichenko There are repetitive people, and there are repetitive people. :-)
  • pranay nallapaneni
    pranay nallapaneni almost 3 years
    '{' is inline with commented for loop. that is the reason it did not compiled. Please try now with the updated code.
  • Abhishek Mane
    Abhishek Mane over 2 years
    @SergeyKalinichenko That also means that it's not necessary to use {} if your for loop contains one statement right ?
  • Abhishek Mane
    Abhishek Mane over 2 years
    don't repeat the same answers which given above.
  • Sergey Kalinichenko
    Sergey Kalinichenko over 2 years
    @AbhishekMane That's correct.