12

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1830323
  • 121
  • 1
  • 1
  • 3

8 Answers8

29

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
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
13
for (i=0;i<5;i++);

is equivalent to

for (i=0;i<5;i++){}
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
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."

ouah
  • 142,963
  • 15
  • 272
  • 331
1

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

    for(i=0;i<5,printf("Hello\n");i++);
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
  • 4
    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]`. – Jonathan Leffler Dec 02 '14 at 17:12
1

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

gre_gor
  • 6,669
  • 9
  • 47
  • 52
0
#include <stdio.h>
    
int main() {
    int sum = 0; // variable initialization  
    int k = 1;  
    //for(int i = 1; i <= 10; i++);
    {
        sum = sum + k;  
        k++;
    }  
    printf("The  value of sum is %d", sum);  
    return 0;
}

if there is a semicolon after for loop then code in the curly braces is treated as regular body and will be executed normally.

In the above program I have commented the for loop and it does the same thing as putting ; at the end of the for loop. It prints the sum as 1 because code inside the curly braces executes once.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

When for loop body contains only single statement then it is not necessary to use curly braces {} around the body of for loop.

for(i=0; i<5; i++)
{
    printf("Napoleon");
    printf("Bonaparte");
}
      
for(j=0; j<5; j++)
    printf("Napoleon");

So for(j=0; j<5; j++); evaluates itself as

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

So this is a for loop with single statement which is ; null statement. null statement does nothing. As 5 times loops run, null statement executes for 5 times but it does nothing as it's definition.

Abhishek Mane
  • 619
  • 7
  • 20
-1

It functions same as :

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

here it has nothing inside the braces and will simply increment the value of i to 5. You can check this using print statement in next line and get i=5.

`Compiled code ss here!!!