Reputation: 564
my question is about how EOF is interpreted in the middle of an input, here is an example:
int main() {
int a, b;
printf("enter something >\n");
scanf("%d", &a);
while((b = getchar()) != EOF) {
printf("%i\n", b);
}
return b;
}
I run the program and enter:
1hello^Z(control+z)abc
the output is:
104 (ascii number for h)
101 (for e)
108 (l)
108 (l)
111 (o)
26 (what is this?)
The digit 1 is read by scanf, the remaining stays in the buffer, getchar() gets all of them until ^Z, which is expected behavior, as the control z closes stdin. however where does 26 come from? If the last thing getchar() reads is EOF why isn't -1 the last value? Also why doesn't this program get out of the loop when it reads ^Z, why do I need to invoke EOF one more time with control z to terminate the loop? 26 is the ascii for SUB, I don't know what to make of this.
Thank you.
Upvotes: 0
Views: 328
Reputation: 71
When the loop ends, b=26 because you entered ctrl+z and this is interpreted as SUB while returning .http://en.wikipedia.org/wiki/Substitute_character
In the ASCII and Unicode character sets, this character(SUB) is encoded by the number 26 (1A hex). Standard keyboards transmit this code when the Ctrl and Z keys are pressed simultaneously (Ctrl+Z, by convention often described as ^Z).
Upvotes: 0