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;
}
The for loop would not get executed at all
The for loop would get executed only once
The for loop would get executed 5 times
The for loop would get executed infinite times
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 4 of 5.

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.

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

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.

Wikiok said:   1 decade ago
for (;;) ==> infinite loop. You don't need a condition

while (1) ; ==> while (1) { ; } ==> while (1) {...} where "..." are program lines.

Sundar said:   1 decade ago
@Wikiok

Yes. You are absolutely correct.

@All

While testing this type of code this system may hang due to infinite loop (press Ctrl+C to exit in DOS).

Ramya said:   1 decade ago
Hi sundhar. Here we don't have any initialization to i.

So how we get infinite loop. Explain me.

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.

Harshit Singh said:   1 decade ago
If semicolon get added in the end of for loop then the for loop will never get executed, hence option A.

Adi said:   1 decade ago
I think printf () is terminated by ;.

So here use it.

Kush said:   1 decade ago
Can anyone explain what exactly does this mean in this question ?
scanf("%s", &i)


Post your comments here:

Your comments will be displayed after verification.