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.
Chakrapani said:
2 years ago
The control never enters into the loop as the semicolon ends it. So, scanf () and printf () functions are called 0 times.
(3)
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?
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?
Harshit Singh said:
1 decade ago
If semicolon get added in the end of for loop then the for loop will never get executed, hence option A.
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!!
Yallaling said:
8 years ago
Yeah, even I also got the same output @Manjula. What is the meaning of that unexpected error occurred?
Ramya said:
1 decade ago
Hi sundhar. Here we don't have any initialization to i.
So how we get infinite loop. Explain me.
So how we get infinite loop. Explain me.
Kush said:
1 decade ago
Can anyone explain what exactly does this mean in this question ?
scanf("%s", &i)
scanf("%s", &i)
Vinuthna said:
1 decade ago
How can we use "%s" in the scanf statement, when the variable declared is of integer type?
Sara said:
9 years ago
scanf("%s", &i)
I didn't get this statement. Can anyone explain me please?
I didn't get this statement. Can anyone explain me please?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers