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.
Sonalika said:
1 decade ago
What if user do not gives any input? how it would run then. !
Priyanka said:
1 decade ago
The for loop ends with a semi-colon, so how can it run infinte times?
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.
Sameer said:
1 decade ago
What if I remove the semicolon?
What will happen?
I heard that semicolon terminates the loop. correct me if I'm wrong.
What will happen?
I heard that semicolon terminates the loop. correct me if I'm wrong.
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.
Vinuthna said:
1 decade ago
How can we use "%s" in the scanf statement, when the variable declared is of integer type?
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 ?
Sachin said:
1 decade ago
Hi all,
The loop will execute only once because it is terminated by using ';' symbol.
The loop will execute only once because it is terminated by using ';' symbol.
Gayathri said:
2 decades ago
In this program if it returns one means what would be the answer?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers