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 1 of 5.
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)
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)
Bhavya said:
4 years ago
Did not understand the scanf statement can someone help?
(1)
Sundar said:
1 decade ago
Hi All,
It is correct. The for loop would get executed infinite times (it reads input from user continuously).
The for-loop will break if condition checking fails.
Syntax: for( [initialization] ; condition ; [increment])
In this program 'scanf("%s", &i)' always return 1 as it reads 1 input from the user. So condition never fails, executed infinite times.
Examples of infinite loops:
while(1); // Infinite loop
while(1) // Infinite loop
{
...
}
for(;1;); // Infinite loop
for(;1;) // Infinite loop
{
...
}
Hope this will help you. Please clarify your doubts and add more comments here. It may help others.
Have a nice day!
It is correct. The for loop would get executed infinite times (it reads input from user continuously).
The for-loop will break if condition checking fails.
Syntax: for( [initialization] ; condition ; [increment])
In this program 'scanf("%s", &i)' always return 1 as it reads 1 input from the user. So condition never fails, executed infinite times.
Examples of infinite loops:
while(1); // Infinite loop
while(1) // Infinite loop
{
...
}
for(;1;); // Infinite loop
for(;1;) // Infinite loop
{
...
}
Hope this will help you. Please clarify your doubts and add more comments here. It may help others.
Have a nice day!
(1)
Jyosna said:
1 decade ago
Here for loop executes only once at that time it will return error while evaluating scanf statement because access specifier is s and it is followed by &i then how loop will execute infinite times.
Keerthana said:
1 decade ago
Here i is an integer but is scanned as a string. Will it not result in error?
Sinju said:
9 years ago
Here, for loop and the scan are linked like a same group, I think for loop can write in a separate fashion, then how it correct, and also for loop can not end with a semicolon.
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?
Sonu Shaw said:
9 years ago
When I run above the code and give as the input 0, 1. Then the output comes 48, 49 like that.
Could you explain, please?
Could you explain, please?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers