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 5 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?
Balaguru said:
7 years ago
for(;scanf("%s", &i); printf("%d\n", i));
In this, when you put semicolon to the for loop it doesn't read that loop.
In this, when you put semicolon to the for loop it doesn't read that loop.
(5)
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.
Bhavya said:
4 years ago
Did not understand the scanf statement can someone help?
(1)
Charanjeet Singh said:
4 years ago
How String(%d) value stored in integer(&i)? Please explain.
(4)
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers