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;
}
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 3 of 5.
Rakesh said:
7 years ago
It is to be replaced with (%d ) not %s.
Rini said:
7 years ago
Here the for loop terminate with semicolon. So it has taken as a simple statement. How can it execute infinite times?
Pragathi said:
6 years ago
In scanf("%s"&I).
Here %s format specifier is used for string. For string no need to take & operator in scanf then how it execute? Explain to me, please.
Here %s format specifier is used for string. For string no need to take & operator in scanf then how it execute? Explain to me, please.
Aayush Chauhan said:
6 years ago
What if we enter 0 input in scanf?
Shyam said:
5 years ago
If we enter 0 then also it should go into infinite loop.
Sandip said:
1 decade ago
Here for loop end with (;)
Is it correct syntax ?
please any body help me...
Is it correct syntax ?
please any body help me...
Muthu selvam said:
1 decade ago
Here the for loop terminate with semicolon. So it has taken as a simple statement. How can it execute infinite times?
Satti said:
1 decade ago
Before initialization how a loop start with semicolon. Obviously wrong ?
Rajeswari said:
1 decade ago
The 'for-loop' declaration is wrong so it will not execute.
Jascow Wendler said:
1 decade ago
Hi Sundar,but the scanf uses %s to read an integer which is wrong or may produce unpredictable results!!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers