I came across the following code :
int i;
for(; scanf("%s", &i);)
printf("hello");
As per my understanding, if we provide integer input scanf would be unsuccessful in reading and therefore return 0, thus the loop should not run even once. However, it runs infinitely by accepting all types of inputs as successful reads.
Would someone kindly explain this behaviour?
That is the incorrect format specifier for an
int: should be"%d".It is attempting to read a string into an
intvariable, probably overwriting memory. As"%s"is specified, all inputs will be read thusscanf()returns a value greater than zero.