printf출력의 궁금점이 있어요

westdrm의 이미지

한컴 리눅스3.0에서
printf("asdf...");
를 실행하면 바로 출력이 되지 않고 '\n'을 만나거나 gets같은 입력받는 함수가 실행이 되어야만 화면에 출력이 되던데요..
바로 출력이 되는 방법이 없을까요?

beta의 이미지

man fflush
를 이용하시거나,
man stderr
표준 에러로 출력하면 unbuffered 로 출력됩니다.

발 담갔다. 이제 익숙해 지는길만이..

wooix의 이미지

참고로 input이랑 output에는 buffer가 있는데 \n이나 flush를 만나야 출력혹은 입력이 완료상태가 됩니다.

때문에 입력시 문제는 fflush(stdin)으로 출력시는 fflush(stdout)으로 해주시면 문제를 잡을수 있습니다.

종종 문제를 일으키는 scanf와 친해지려면 fflush는 필수일듯하군요.. :)

즐프~~

평온하다~

전웅의 이미지

scanf() 로 발생할 수 있는 문제를 fflush(stdin) 으로 해결하려는 태도는
매우 위험합니다. fflush() 는 output stream 에 대해서만 그 행동이
정의되어 있습니다.

http://c-expert.uos.ac.kr/board/hclc_post_board/ScB.cgi?process=disp_data&datanum=521

를 참고하시기 바랍니다.

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

lsj0713의 이미지

재미있는 것은, Visual C++에서는 fflush(stdin)이 정의되어 있다는 것입니다.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_fflush.asp

그러나 Visual C++에 한정된 이식성없는 코드일 뿐이고, 또한 전웅님께서 지적하신 문제점이 있기 때문에, fflush는 output에만 사용하는 것이 현명합니다.

Mins의 이미지

이전에 fflush 에 대해 뉴스그룹에서 본 내용이 생각나 올려봅니다. :)

http://groups.google.co.kr/groups?dq=&hl=ko&lr=&ie=UTF-8&inlang=ko&newwindow=1&threadm=3E2681AD.64BCA28D%40bawi.org&prev=/groups

PS. 올려놓고 보니까.. 위의 글과 비슷한 글이네요.. -_-;;

lethean의 이미지

그냥 쉽게 프로그램 시작시 setbuf(stdout, NULL); 을 호출해주시면 됩니다. 이유는 윗분들이 다 적어놓으셨네요...

Nevermind... :)

댓글 달기

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