C Programming - Control Instructions - Discussion


What will be the output of the program?

int main()
    int i=0;
    for(; i<=5; i++);
        printf("%d", i);
    return 0;

[A]. 0, 1, 2, 3, 4, 5
[B]. 5
[C]. 1, 2, 3, 4
[D]. 6

Answer: Option D


Step 1: int i = 0; here variable i is an integer type and initialized to '0'.
Step 2: for(; i<=5; i++); variable i=0 is already assigned in previous step. The semi-colon at the end of this for loop tells, "there is no more statement is inside the loop".

Loop 1: here i=0, the condition in for(; 0<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 2: here i=1, the condition in for(; 1<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 3: here i=2, the condition in for(; 2<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 4: here i=3, the condition in for(; 3<=5; i++) loop satisfies and then i is increemented by '1'(one)
Loop 5: here i=4, the condition in for(; 4<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 6: here i=5, the condition in for(; 5<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 7: here i=6, the condition in for(; 6<=5; i++) loop fails and then i is not incremented.

Step 3: printf("%d", i); here the value of i is 6. Hence the output is '6'.

Sri said: (Sep 7, 2010)  
The for loop in this program has a semi colon at the end of statement means their is no statements in loop to repeat after complition of loop the incremented value is assigned, here I takes 6 and it is displayed.

Arjun Reddy said: (Dec 8, 2010)  
After satisfying everytime the condition of i<=5 for the values i=0,1,2,3,4,5,

It prints the value of i. So answer is A.

Sundar said: (Mar 5, 2011)  
@Arjun Reddy

You are wrong. The given answers is correct only.

Please note the semi-colon in the for-loop:

for(; i<=5; i++); // <-- note this semi-colon.
printf("%d,", i); // printf is NOT a part of for-loop, it is next stmt to for-loop.

If you remove that last-semi-colon in the for loop, the it will become like :

for(; i<=5; i++)
printf("%d,", i); // this printf is only the part of for-loop.

Now it will work as you said. The main purpose of this question is to check whether are you able to find the above difference.

Hope this help you. Have a nice day!

Vinoth M said: (Mar 13, 2011)  
Thank you sundar very good explanation.

Vaibhav said: (Jul 5, 2011)  
Pay attention towards for loop , there is semicolon at the end of for loop .It means the loop will execute for given condition but control will remain at the same line till the loop get exhausted. Hence when control passes to next line the value of I become 6.

Puspita said: (Jul 17, 2011)  
Thanks sundar.

Shaswati said: (Nov 13, 2011)  
Many many many thanks to indiabix for providing an online compiler...its really very much helpful ..thanks a lot

Manish said: (Dec 10, 2011)  
This program has been output 5 because increment 5 times.

Priyanka said: (Jul 3, 2012)  

What you are thinking is wrong. Because after repeating for the 5th time I satisfies the condition because you have i<=5. So the condition is correct and I will be incremented further and its value become 6. Your assumption is true when the condition is i<5.

Ayush Gupta said: (Jan 29, 2015)  
Even if you don't initialize i with zero it will print 6. So does for loop initialize loop variable by 0?

Vikash said: (May 2, 2015)  

Value of 'i' will be printed every time when the the loop execute.

Mohit said: (Oct 7, 2015)  
I can't understand C programming please some body help.

Hemant Kumar said: (May 13, 2016)  
In C when an expression is ended with a semicolon(;) it becomes an statement. But when we put only a semicolon(;), it is called as a NULL statement.

Explanation to this problem:

Well the statement for(; i<=5; i++); will be treated as

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


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

So when the loop will start it will take the initial value as 0 and it will check the condition(i<=5) as the condition is true it will execute the body of for loop but as it a null statement( ;) it will do nothing and the control will be transferred to the update expression(i++). So repeating the steps until the value of i becomes 6.

Now as i=6 so the condition (i<=5) will return false and the control will jump out of the for loop and the next statement following the for loop will be executed i.e printf("%d", i);

So it will print the value as 6 and then the program will be terminated successfully as it is returning a zero to the int main.

Tongla said: (May 19, 2016)  
The output will be 0, 1, 2, 3, 4, 5

Aravind Kumar said: (Nov 15, 2016)  
Thanks for your explanation @Sundar.

Albert Khan said: (Nov 18, 2016)  
After for loop, we should not use the semi colon.

Anonymous said: (Aug 11, 2017)  
Thanks @Sundar.

It was really helpful.

Naresh said: (Aug 13, 2017)  
The value of i i 12345 ifwe run the programme.

Ratna Priya said: (Sep 27, 2017)  
Loop is going to 0 to 5 loop satisfied up to 5 then print all the no. When loop doesn't satisfy then the loop will terminate its value.

So, the correct option is A.

Dinesh said: (Dec 22, 2017)  
Great explanation @Sundar.

Bhuvanesh said: (Aug 13, 2018)  
Thank you @Sundar.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.