C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 1)
1.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=0;
    for(; i<=5; i++);
        printf("%d", i);
    return 0;
}
0, 1, 2, 3, 4, 5
5
1, 2, 3, 4
6
Answer: Option
Explanation:

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

Discussion:
21 comments Page 1 of 3.

Bhuvanesh said:   7 years ago
Thank you @Sundar.

Dinesh said:   8 years ago
Great explanation @Sundar.

Ratna Priya said:   8 years ago
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.
(8)

Naresh said:   8 years ago
The value of i i 12345 ifwe run the programme.
(1)

Anonymous said:   8 years ago
Thanks @Sundar.

It was really helpful.
(1)

Albert khan said:   9 years ago
After for loop, we should not use the semi colon.
(1)

Aravind kumar said:   9 years ago
Thanks for your explanation @Sundar.

Tongla said:   9 years ago
The output will be 0, 1, 2, 3, 4, 5
(2)

Hemant Kumar said:   9 years ago
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++)
;

or

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

Mohit said:   10 years ago
I can't understand C programming please some body help.
(1)


Post your comments here:

Your comments will be displayed after verification.