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.
Gayathri said:
2 decades ago
In this program if it returns one means what would be the answer?
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.
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)
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!!
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.
Akhilesh said:
1 decade ago
hi all...
while(1); //this is Infinite loop its ok..i got
while(1) //dis is also Infinite loop bt how i m not geting plz explain more
{
...
}
Thankx
while(1); //this is Infinite loop its ok..i got
while(1) //dis is also Infinite loop bt how i m not geting plz explain more
{
...
}
Thankx
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.
Wikiok said:
1 decade ago
for (;;) ==> infinite loop. You don't need a condition
while (1) ; ==> while (1) { ; } ==> while (1) {...} where "..." are program lines.
while (1) ; ==> while (1) { ; } ==> while (1) {...} where "..." are program lines.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers