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의 이미지

답변 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.