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.
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)
Shiva said:
1 decade ago
Hi Ramya,
For loop syntax is: for(;;)
Here we have three places for initialization, condition and increment/decrement.
These are choices, if we dont write any one from those three. There is no problem in execution.
In our program there is no initialization part so it will not affect to the program, it just checks the condition and it executes both condition, increment/decrement parts. It wont see about initialization, if it is there it will execute.
So the program is correct.
For loop syntax is: for(;;)
Here we have three places for initialization, condition and increment/decrement.
These are choices, if we dont write any one from those three. There is no problem in execution.
In our program there is no initialization part so it will not affect to the program, it just checks the condition and it executes both condition, increment/decrement parts. It wont see about initialization, if it is there it will execute.
So the program is correct.
Sundar said:
1 decade ago
@Jascow
Yes of-course.
The printf statement will always print unexpected values only. But gets the input from the user (via keyboard) and prints some unexpected values... gets value then prints ... and so on infinitely.
I have tested it before posting my comments here. So, I hope this may be help you.
Yes of-course.
The printf statement will always print unexpected values only. But gets the input from the user (via keyboard) and prints some unexpected values... gets value then prints ... and so on infinitely.
I have tested it before posting my comments here. So, I hope this may be help you.
Manjula said:
8 years ago
@All.
After executing this same program, I got this error " Unexpected error occurred. Please try again after few seconds." it is showing error in scanf and printf.
Actually, it's not executing the for loop so may be the answer is not correct please check and give me the solution.
After executing this same program, I got this error " Unexpected error occurred. Please try again after few seconds." it is showing error in scanf and printf.
Actually, it's not executing the for loop so may be the answer is not correct please check and give me the solution.
Sebastinrichard.j said:
1 decade ago
1.how this scanf statement uses %S to read integer values,
2.Syntax: for( [initialization] ; condition ; [increment])
if above is the syntax , there is no semi-colon at the end of syntax, bt, in the program for loop ends with semi-colon. can any1 xplain this.
2.Syntax: for( [initialization] ; condition ; [increment])
if above is the syntax , there is no semi-colon at the end of syntax, bt, in the program for loop ends with semi-colon. can any1 xplain this.
Krati said:
1 decade ago
@priyanka.
For loop with a ; execute condition until it fail but loop with a ; never execute statement next to for loop that's why for loop in this example execute until it fail but there is so such condition and that's why execute infinite times.
For loop with a ; execute condition until it fail but loop with a ; never execute statement next to for loop that's why for loop in this example execute until it fail but there is so such condition and that's why execute infinite times.
Naveen said:
1 decade ago
Easy guys.
Semicolon terminates for loop removing semicolon results in error.
Its is for loop as for as the condition is correct it keep on executing ie in this case printing the values so infinite loop.
@sonalika.
It waits for the input.
Semicolon terminates for loop removing semicolon results in error.
Its is for loop as for as the condition is correct it keep on executing ie in this case printing the values so infinite loop.
@sonalika.
It waits for the input.
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.
Divya said:
8 years ago
Hi everyone, I just checked the output and the above code is not taking any input from user instead it is printing 5 infinite times. I couldn't understand this. Can anyone explain how this will work?
Ayush Rai said:
1 decade ago
But it can be possible that the value entered for i maybe 0 then the condition can exit or somehow scanf("%s", &i) may result in 0 depending upon what user input is ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers