EOF reading C/C++
I'm using NetBeans MinGW to compile simple c programs(I'm new at this). My problem is that I have this simple code
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int c,i=0;
while((c=getchar())!=EOF){
i++;
}
printf("%d",i);
return 0;
}
and when I try to end input like this:
hello^Z [enter]
it doesn't do it, I need to re-enter
^Z[enter]
to end it.
I'd appreciate开发者_开发问答 that you tell me why this happens.
Thanks in advance
C input is line-oriented by default. Ending a line with the EOF character (^Z
on Windows, ^D
on Unix) ends the line (without a trailing newline) but doesn't actually signal end of file; the end of file condition is signaled when it's encountered on the next read, i.e. at the beginning of a line.
Just the way the console handles input
Ctrl-Z on a UNIX system would be an interrupt to let you suspend the process, so I guess it is a Windows console.
When you Ctrl-Z after the characters it probably does treat this as an "End" which is Ctrl-Z on its own.
精彩评论