What is the output of the following C program?
#include <stdio.h>
int main(void) {
FILE *fp = tmpfile();
if (fp == NULL)
return 1;
fputs("abc", fp);
rewind(fp);
int count = 0;
while (!feof(fp)) {
fgetc(fp);
count++;
}
printf("%d\n", count);
fclose(fp);
return 0;
}
After reading 'c', feof is still false – the end is detected only by the next, failing fgetc, which is counted too. Loop on fgetc(fp) != EOF to get 3.