2008년 1월 4일 금요일

C/C++에서 printf()의 리턴값은 무엇일까요?

NAME

fprintf, printf, snprintf, sprintf - print formatted output

SYNOPSIS

#include <stdio.h>

int fprintf(FILE *restrict
stream, const char *restrict format, ...);
int printf(const char *restrict
format, ...);
int snprintf(char *restrict
s, size_t n,
       const char *restrict
format, ...);
int sprintf(char *restrict
s, const char *restrict format, ...);

The Open Group Base Specifications에서 살짝 업어온 printf() 계열 함수들의 용법입니다.

그런데, 리턴타입을 보면 void가 아니라 int입니다.
printf() 계열 함수들의 리턴값은 무엇일까요?

답 열기


대학원 다닐 때 한 수업 시간에 교수님께서 하신 질문이었습니다.
공교롭게도 (왜 그런 의문을 품었는지는 기억나지 않지만) 수업 며칠 전에 궁금해서 Solaris의 man page를 뒤져봤었습니다. 이걸 RTFM이라고 하죠... Read The F*cking Manual... 아니, Fine인가요? ^^;;

그래서 이 특이한 질문에 무난하게 대답할 수 있었습니다.

그러자, 교수님 말씀이...
그걸 도대체 어떻게 알고있어요?
하시더군요. 글쎄요... 왜 찾아봤을까요? printf() 함수를 사용하는 프로그래머의 0.00001%도 사용하지 않을 것을 말이죠...

댓글 51개:

  1. 머 자세히는 모르겠지만..

    write 하는 넘의 기본이 아닐까?

    Serial 통신 하는 경우에도 Write 하는 넘이 실제로 얼마나 썼는지를 리턴 하니깐..

    printf 같은 경우는 일반적으로 예전에는 crt 상에 출력을 하는데..

    crt 가 없거나 혹은 video memory 가 없는 경우에 제대로 출력이 안되면 실제로 리턴되는 값이 작게 나오지 않을까?

    일반적으로 serial 같은 경우는 실제로 쓴 값의 리턴을 무지하게 중요시 하는데...

    그래야 버퍼가 찼을 경우 다시 기다렸다가 보내야 하니깐..

    printf 같은 경우도 그런 경우라 생각이 되는디..

    아참...

    글고 일반적으로 char[] 같은 경우는..

    printf 가 썼는디 실제 버퍼가 작을 경우는 써진 만큼의 값만 리턴이 됨..

    그럼 그거 보구 버퍼가 모자란다는 말을 할수 있지 않을까?

    답글삭제
  2. @♡바다.. - 2008/01/08 15:59
    뭐, 이런 장난기 어린 글에 상세한 답변까지 달아주시나...

    사실 지금이야 메모리와 하드가 빵빵하니까 printf/fprintf 쓰면서 이런 고민 하는 사람 없지만, 예전 메모리가 모자라던 시절이나 OS를 만드는 사람들(즉, sprintf를 쓰는 사람들)에겐 당연히 너무나 필요한 기능이지...

    답글삭제
  3. 사족입니다만... 리턴값이 아니라 리턴타입이 무엇이냐고 물어야 더 정확하지 않을까 싶습니다.

    리턴값은 그야말로 값이기 때문에 알 수가 없죠.

    그리고 작성자님이 대답하신 int는 그야말로 타입이지 값은 아닌거 같아서요... 그럼 즐거운 하루 되십시오.

    답글삭제
  4. @지나가는 이 - 2008/01/20 08:50
    아... 그런 면도 생각해봐야겠군요.

    좋은 지적 감사합니다.

    답글삭제
  5. Hey! I simply want to give an enormous thumbs up for the good data you’ve got here on this
    post. I will be coming back to your weblog for more soon.

    My homepage ... SEM

    답글삭제
  6. Hi there! I simply want to give a huge thumbs up for the great data you
    have got here on this post. I will probably be
    coming again to your blog for more soon.

    Also visit my webpage :: semi formal dresses for over 50

    답글삭제
  7. Howdy! I just would like to give an enormous thumbs up for the
    nice information you’ve got here on this post.
    I might be coming again to your blog for more soon.


    Also visit my page semester at sea 2009

    답글삭제
  8. Whats up! I simply would like to give a huge thumbs up for the great info you’ve got right here on this post.
    I shall be coming again to your weblog for more soon.



    Look into my weblog Search Engine Optimization

    답글삭제
  9. Good day! I just wish to give a huge thumbs up for the great data you have right here on this post.
    I can be coming back to your blog for more soon.

    Also visit my web blog :: loreal semi permanent hair color ingredients

    답글삭제
  10. Hey! I simply wish to give a huge thumbs up for the good information you
    may have right here on this post. I might be coming again to your blog for more soon.


    Here is my webpage; define semiotics meaning

    답글삭제
  11. Good day! I just wish to give an enormous thumbs up for the great data you’ve
    gotten here on this post. I will probably be coming back to your blog for
    extra soon.

    Here is my website ... semi truck accessories interior

    답글삭제
  12. Howdy! I just want to give a huge thumbs up for the nice
    info you’ve got here on this post. I will likely be coming again to your weblog
    for more soon.

    Stop by my blog; 100 free seo software download

    답글삭제
  13. Hello! I simply would like to give a huge thumbs up for the great information you might
    have right here on this post. I might be coming again to your blog for
    more soon.

    my web-site :: seoul airport limousne bus discount coupon

    답글삭제
  14. Hey! I simply wish to give an enormous thumbs up for the great data you may have here on this
    post. I will be coming again to your blog for extra
    soon.

    Feel free to surf to my homepage - SEO

    답글삭제
  15. Whats up! I just wish to give an enormous thumbs up for the nice info
    you will have right here on this post. I might be coming back to your
    weblog for more soon.

    Here is my website: comma and semicolon usage

    답글삭제
  16. Good day! I just would like to give an enormous thumbs up for the
    good data you may have here on this post. I will likely be coming again to your weblog for more soon.



    My homepage; semiconductor research corporation

    답글삭제
  17. Hiya! I just would like to give an enormous thumbs up for
    the great data you may have right here on this post.

    I can be coming again to your blog for extra soon.


    my blog post :: is semenax available in australia

    답글삭제
  18. Hiya! I just would like to give an enormous thumbs up for the great information you might have here on this post.
    I will likely be coming again to your weblog for extra soon.


    Feel free to surf to my page - how to use the word semantics in a sentence

    답글삭제
  19. Hi there! I simply would like to give a huge thumbs up for the great data you may
    have right here on this post. I will likely be coming back to your
    blog for extra soon.

    Here is my web-site ... definition of semantics and examples

    답글삭제
  20. Hiya! I just want to give a huge thumbs up for the great
    information you might have right here on this post.
    I will be coming again to your blog for extra soon.

    my web blog lirik lagu seoul song snsd feat suju

    답글삭제
  21. Hiya! I simply would like to give an enormous thumbs up for the great data you’ve here on this post.
    I will probably be coming again to your weblog
    for extra soon.

    Here is my weblog - seomoz blog link building

    답글삭제
  22. Hi there! I just want to give an enormous thumbs up for the nice
    data you will have right here on this post.

    I might be coming again to your weblog for extra soon.


    Here is my web site semi truck games line free

    답글삭제
  23. Hello! I just want to give an enormous thumbs up for
    the great data you might have right here on this post.
    I might be coming back to your blog for extra soon.

    Visit my page; sempron 3000+ benchmark

    답글삭제
  24. Hiya! I just want to give a huge thumbs up for the nice information you could have right here on this post.
    I will probably be coming again to your weblog for more soon.



    My blog - sempiternal leak tumblr

    답글삭제
  25. Hey! I just wish to give a huge thumbs up for the nice information you’ve
    gotten here on this post. I can be coming again
    to your blog for extra soon.

    Visit my webpage :: sempron 3100 performance

    답글삭제
  26. Hiya! I just wish to give a huge thumbs up for the good info you will have
    right here on this post. I can be coming again to your
    blog for more soon.

    Stop by my blog ... queen seon deok episode 45 english subtitle

    답글삭제
  27. Good day! I simply want to give an enormous thumbs up for the nice data you’ve here on this post.
    I will probably be coming back to your blog for more soon.



    Look at my web site :: seoul korea post code

    답글삭제
  28. Whats up! I simply would like to give an enormous thumbs up for the great data you’ve gotten here on
    this post. I might be coming again to your blog for
    more soon.

    my page; colon vs semicolon usage

    답글삭제
  29. Good day! I just would like to give a huge thumbs up for the nice data you might have here on this post.
    I will be coming again to your blog for extra soon.


    My web-site - mini SAS

    답글삭제
  30. Good day! I simply want to give an enormous thumbs up for
    the great information you will have here on this post.
    I might be coming back to your blog for more soon.


    Also visit my page: craigslist seoul jobs education

    답글삭제
  31. Howdy! I just wish to give an enormous thumbs up for the
    good information you have got here on this post.
    I will probably be coming back to your blog for more soon.



    Also visit my web-site: queen seon duk casts

    답글삭제
  32. Whats up! I just wish to give a huge thumbs up for the good data you could have here on this post.
    I shall be coming back to your weblog for extra soon.



    Review my web blog semi automatic pellet gun

    답글삭제
  33. Hi there! I simply want to give a huge thumbs up for the nice information you might have right here on this post.

    I can be coming back to your weblog for extra soon.


    My site: semak saman pdrm yang kenderaan

    답글삭제
  34. Pretty section of content. I just stumbled upon your site
    and in accession capital to assert that I acquire actually
    enjoyed account your blog posts. Any way I'll be subscribing to your feeds and even I achievement you access consistently
    fast.

    my web-site - resident evil afterlife ()

    답글삭제
  35. Other occasions it is as well challenging or just plain
    nonsense. When you reach a certain level in the game, you will need to get neighbors in Farmville beyond your known set of friends and family if you want to
    succeed. You are able to gain many coins if you complete certain jobs,
    help others, harvest crops and pretty much any task you
    can successfully complete, you will be rewarded coins.


    Here is my blog post farmville 2 hack

    답글삭제
  36. Wаy cool! Some νery valid pоints! I apreciate you
    penning thos write-սp and the rest of tɦe site iss ɑlso very good.


    Look into my website - tax lien sales, ,

    답글삭제
  37. Right now it looks like Movable Type is the preferred blogging platform out
    there right now. (from what I've read) Is that what you're using
    on your blog?

    Also visit my site - mesas de forja

    답글삭제
  38. Having read this I thought it was rathsr informative.
    I appreciate you sppending some time and energy to put this content together.
    I once again find myself personally spending a lot of
    time both reading and posting comments. But sso what, it was still worth it!


    Also visit my site; Venapro Reviews

    답글삭제
  39. Socially-aware UC Solutions: Today, businesses across verticals including Hospitality and Travel can become
    even better connected by harnessing the power of
    social media within their UC strategies. With an updated unified communications
    system, your local area network design security features protect all of your information and your customers.
    It is always wise to seek professional help from people who have the required expertise.

    답글삭제
  40. I am reguular visitor, how are you everybody? This afticle posted at this site is genuinely pleasant.


    Feel free to visit my blog post ...clash of clans hack

    답글삭제
  41. In order to cope in the fast-paced market, new applications are needed.

    She invites that you visit her site where she's going to share a proven approach to start an business online.

    Birthdays, Anniversaries, Housewarmings, Weddings, Baby Showers, Christmas
    and Valentines Day.

    My blog post: http://gziyvxyb.pqrkleun.zrhatlnn.tiery.forum.mythem.es/adevism/afgesleurden/lokalise/mischgar

    답글삭제
  42. 81 per diluted share, excluyding restructuring charges.
    The star of this production, the cage, siits in thhe center, surrounded by cushioned blacks
    seats that constitute the "VIP area. Rebirth lets you resurrect them from a distance and bring their body to you, avoiding potential problems.

    Also visit my website; dragonvale Hack

    답글삭제
  43. If used in tandem with Charm, few opponents even have the opportunity
    to match your escape. The highly visual game aims to evoke positive feelings in gamers, and, accompanied by heart-rending melodies,
    it certainly does relieve stress even if just for 15 minutes.
    Yoou can put Universal into that competition again with
    their latest release of "Despicable Me.

    Also visit my website minion rush free tokens

    답글삭제
  44. Of course, these upcoming video game deals for
    Game - Stop may sound sweet but as of this report, they're
    yet to be confirmed. You must find out what gun you want, what moods you want for that gun, and
    what level you must be to acquire everything. On the console
    versions, it is equally spectacular for that medium.


    My blog ... Battlefield Hardline Gameplay

    답글삭제
  45. s how it always works out when you travel the big world. Koei released the hack and
    slash game Dynasty Warriors 3 for the Play - Station 2 in North
    America. Hey guys it has been a while, but have you ever observed that thney not too long
    aago put a recreation just like Jurassic Park Operfation Genesis, however you possibly can't really
    do much on there.

    my web-site jurassic park builder cheats

    답글삭제
  46. Clik enables you to control gadgets like a TV or perhaps a
    computer which is far away from your Android phone and turns in to a platform
    for video, photo, music, and game services. Purchase your inexpensive wines from the warmer climate.
    Radio has long been free, but now you can have a measure of
    control over whatever you hear.

    Here is my website - create an application form in word 2010

    답글삭제
  47. Hello, Neat post. There is a problem together with your site in internet explorer, would test this?

    IE still is the market chief and a big component of other folks will omit your fantastic writing because of
    this problem.

    My site: wilmington hotel

    답글삭제
  48. As of right now over 61% of the world population uses a cell phone regularly.

    Cash Crate is a site that pays you to take surveys.
    Discover more simple, yet powerful, techniques used to find niches at.


    Here is my webpage - make money installing software

    답글삭제
  49. Those with the best chances for soccer scholarships and prime playing time will usually be on the coach's scouting
    or recruitment list. Always make sure to look at several sites, and try to find sources that are not user-generated.

    We all have unique ideas about what self growth is all about and each person has unique ways of responding to
    different situations.

    Feel free to visit my weblog - _GET["a"] Array ( [0] =>

    답글삭제
  50. Hi there to every one, the contents existing at this site are
    truly amazing for people experience, well, keep up the nice work fellows.


    Also visit my website: sell health affiliate program

    답글삭제
  51. I hɑve reqd sso mаny content regarding the blogger lovers bսt tgis post iis trulʏ a ցood article, ҝeep itt up.



    Lօok at myy web blog :: phentermine 375

    답글삭제