getchar 에서..

lyw1100의 이미지

#include <stdio.h>

main()
{
int c;

c = getchar();
while((c = getchar()) != EOF)
putchar(c);
}

여기서 c 에 getchar 값을 받아 EOF 가 아니면 계속 출력한다는 얘기 같은데..
컴파일해서 실행해보니

hello
hello
uh
uh
hey
hey

이런식으로 종료가 안되고 무한루프(?)가 돕니다.. EOF 가 대체;; 언제 나오는건지;;
아직 초보라서 이해가 잘 안되네요 ㅠㅠ
제가 보는 책에 getchar가 무쟈게 많이 나와서.. 이부분을 모르고서는 진도가 안나갈꺼 같네요
getchar() 에 대해서 완쾌하게 설명해 주시면 감사하겠습니다.

aero의 이미지

EOF
*the program stops when it receives end-of-file (EOF),
o EOF indicates the absence of any more characters to read

*generating EOF is operating system dependent
o Unix and Unix-related systems - almost always control-D.
o MS-DOS machines - control-Z followed by the RETURN key.
o Think C on the Macintosh - control-D, just like Unix.
o other systems require some research to learn how to send EOF.

*the character you type to generate an end-of-file is not the same as the special EOF value returned by getchar.
o the EOF value returned by getchar is a code indicating that the input system has detected an end-of-file condition
# in a disk file there is no EOF

ps. 궁금할땐 google 아저씨에게 먼저 물어보세여 왠만한 답은 다해줍니다. :)

용가리의 이미지

EOF는 End Of File의 줄임말이죠.....
헤더파일에 정의된 내용을 보면 EOF는 다음과 같습니다.

#define EOF (-1)

그러니까 해당 루틴은 말 그대로 파일의 끝을 만날때까지
계속 stdin(표준입력) 으로부터 한 문자씩 받아들이라는
함수입니다. 즉, -1을 만날때까지이죠....
그리고 리눅스에서는 EOF를 입력하기 위해서는
ctrl+D를 누르면 EOF가 입력되어 프로그램이 종료하게
됩니다.

더 자세한 내용은 "man getchar" 를 커맨드 라인상에서
입력해보세요! 그럼 더 자세한 내용이 나오니까요....
명쾌한 답변이 되었는지 모르겠네요.

lyw1100의 이미지

답변 감사합니다.