scanf 에 대해 문의합니다.

keaton32의 이미지

#include

int main()
{
int num = 9;

while(1)
{
printf("Enter number : ");
fflush(stdin);
scanf("%d\n", &num);
printf("num = %d\n", num);

if (num == 10)
break;
}
return 0;
}

num을 입력하면 바로 출력값이 나오지 않고 다시 입력시에 전의 출력값이 나옵니다.
왜 그런지??
num을 입력하여 바로 출력값이 나오게 할려면 어떻게 해야 하는 지 아시는 분있으시면 알려주시기 바랍니다. ^^;

gamdora의 이미지

scanf("%d\n", &num);을 scanf("%d", &num);으로 바꾸면 됩니다.

그런데 fflush(stdin);과 같이 표준 입력을 플러시하는 건

하면 안 된다고 알고 있습니다.

keaton32의 이미지

아..감사합니다.
그런데 왜 그런건지??
이유좀 알수 없을까요??

mrhyunby의 이미지

scanf("%d\n", &num);

에서 \n을 지우면 바로바로 나오는데요??

제 생각인데;
%d\n하면

5[enter]6[enter] 이런식으로 치잖아요
아마 %d \n은 5와 [enter]를 인식하게 되는거 같습니다..
즉 5다음에 입력되는 [enter]를 버퍼에 담지않는다거나.ㅠㅠ
아 너무 어렵네요.ㅎㅎ
ㅠㅠㅠ "%d" 하면 되긴하네요..

구지 fflush 까지 쓸 필요도 없을듯 하구요..

keaton32의 이미지

아..그리고

scanf로 입력받는 경우
원래 값을 받기위해 넌 블럭킹상태로 들어가야 하는 거 아닌가요??
그런데 바로 값을 읽지 않고 무시하고 넘어가 버리는 경우 가 있는 그때는 어떻해야 하나요??

int run_flag;
..................
...............
........

run_flag = 1;
..................
...............
........

printf("Do u wanna continue?? (a or b) : ");
fflush(stdin);
scanf("%d", &run_flag);
printf("\n");
printf("Run_flag =%d", run_flag);

scanf() 명령어 라인에서 입력값을 받아야 하는데 무시하고 그냥 전에 입력되어 있던 1이 출력됩니다.
어떻게 해야 scanf()에서 입력받을 수 있는 건지??
아시는 분 있으시분 답변 부탁드립니다.

전웅의 이미지

scanf 함수를 "사용"해 입력을 받으려 시도하시기 전에 scanf 함수의
행동을 좀 더 자세히 알아보시기를 권합니다 - 1과 함께 입력된 "엔터"
(정확히는 newline)이 scanf() 에 어떤 영향을 주는지 고민이 필요
합니다.

scanf 함수 자체가 사용자의 interactive input 을 바로 받아 처리하기
위한 용도로 설계된 함수는 아닙니다. 하지만 어렵긴 하지만 스트림과
scanf 함수의 동작을 이해한 후 천천히 과정을 따라가면 기대하는대로
동작하지 않는 이유를 파악하실 수 있으리라 생각합니다 - 그 과정에서
입력이 무시되는 듯한 이유가 blocking 과는 무관하다는 사실도 아실 수
있을 겁니다.

그리고 위에서도 언급되었듯이 fflush(stdin); 은 표준에 따르면 "잘못된"
것입니다.

--
Jun, Woong (woong at icu.ac.kr)
Web: http://www.woong.org (서버 공사중)

--
Jun, Woong (woong at gmail.com)
http://www.woong.org

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.