Reputation: 1517
I have some experience with the c programming but sometimes the author of some books just pose such strange syntactical question which we will never use in actual programming and similar to that I stuck on one such question though the answer is given but I am not able to understand the answer.Here is the code below
int main
{
int i;
for(;scanf("%d",&i);printf("%d",i)){
;
}
}
The question is how many times this for loop will run and the answer given is indefinite Can anyone explain how the loop will get executed
Upvotes: 3
Views: 160
Reputation: 154065
After accept answer
Yes this very well may be an infinite loop.
scanf()
returns the number of successfully scanned fields (0 or 1 ) here or EOF
on IO error or end-of-file.
The only way to exit the loop is to enter a non-numeric input like "xyz".
Should user close stdin
, scanf()
will return EOF
. EOF
is a negative value (not 0). So the loop will continue calling scanf()
, continue receiving EOF
until the program is killed.
#include <stdio.h>
int main(void) {
int i;
for (; scanf("%d", &i); printf("%d", i)) {
;
}
return 0;
}
Upvotes: 0
Reputation: 134356
No. This is not an infinite for
loop.
As per the syntax of for
loop, from section 6.8.5.3 of the n1256 TC3 C99
for ( clause-1 ; expression-2 ; expression-3 ) statement
behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body....
In your case, expression-2 is scanf("%d",&i)
, and the return value of scanf()
will be considered for evaluation of expression-2.
As per the man page of scanf()
Return Value
... return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
So, in your case,
till time scanf()
is success, it will number of input items
successfully matched
, which is 1
and a TRUE
value, for
loop
will continue execution.
if the scanf()
fails [example: input char
value], the return
value of scanf()
will be 0
[matching failure] and the loop will
terminate.
Upvotes: 1
Reputation: 1776
Here, indefinite means that it is cannot be defined(unknown), it does not mean infinite (just to clear things out). Because, here the no of times the loop is run is dependent on the input given. If a correct input is given, which is an integer, the loop keeps running. If a wrong input is given, that is a non integer, the control jumps out of the loop. So you cant have a single answer for all scenarios, hence it is indefinite.
Upvotes: 2
Reputation: 4041
Scanf will return the no of successful input get. If you give the input correctly then the condition will be true so it keep on running as indefinite.
To make the condition false you can give the input other than the integer value. That time scanf will return zero. So the loop will end.
Upvotes: 3
Reputation: 19874
scanf
returns number of elements successfully read and in this case it will be 1 if it reads a integer and keeps printing it.
Let's say you pass a character here then scanf()
fails and exits from a for
loop
Upvotes: 6