C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 11)
11.
In the following program how long will the for loop get executed?
#include<stdio.h>
int main()
{
    int i=5;
    for(;scanf("%s", &i); printf("%d\n", i));
    return 0;
}
The for loop would not get executed at all
The for loop would get executed only once
The for loop would get executed 5 times
The for loop would get executed infinite times
Answer: Option
Explanation:

During the for loop execution scanf() ask input and then printf() prints that given input. This process will be continued repeatedly because, scanf() returns the number of input given, the condition is always true(user gives a input means it reurns '1').

Hence this for loop would get executed infinite times.

Discussion:
49 comments Page 5 of 5.

Sonalika said:   1 decade ago
What if user do not gives any input? how it would run then. !

Rajeswari said:   1 decade ago
The 'for-loop' declaration is wrong so it will not execute.

Shyam said:   5 years ago
If we enter 0 then also it should go into infinite loop.

Bhavya said:   4 years ago
Did not understand the scanf statement can someone help?
(1)

Anu said:   8 years ago
I am not getting this, please give a clear explanation.

Adi said:   1 decade ago
I think printf () is terminated by ;.

So here use it.

Vishi said:   8 years ago
So, which is the correct answer?

Option A or D?

Rakesh said:   7 years ago
It is to be replaced with (%d ) not %s.

Aayush Chauhan said:   6 years ago
What if we enter 0 input in scanf?


Post your comments here:

Your comments will be displayed after verification.