How does printf() works

Discussion in 'C Programming' started by Sanchit, Mar 22, 2008.

  1. Sanchit

    Sanchit Guest

    I want to know how does printf (stdio library function) works? Does
    this depand on complier (I am using gcc on Linix)

    Does it uses some buffer in which it stores all what needed to be
    printed and in end of program it prints that or something else.
    Sanchit, Mar 22, 2008
    #1
    1. Advertising

  2. Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
    Sanchit <> napisa³(a):

    > I want to know how does printf (stdio library function) works? Does
    > this depand on complier (I am using gcc on Linix)

    Uhm. It depends on implementation (so on the library not the compiler
    itself).

    > Does it uses some buffer in which it stores all what needed to be
    > printed and in end of program it prints that or something else.


    You'll best just grab the glibc source and find this implementation.


    --
    Tomasz bla Fortuna
    jid: bla(at)af.gliwice.pl
    pgp: 0x90746E79 @ pgp.mit.edu
    www: http://bla.thera.be

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.7 (GNU/Linux)

    iD8DBQFH5PKhT6wvGJB0bnkRAucXAJ9MXo8HWUUBIAenKRAWW0FKCHrW5ACgnzXR
    2PyUKHQKgpmKh83NlFQjHKM=
    =2kU+
    -----END PGP SIGNATURE-----
    Tomasz bla Fortuna, Mar 22, 2008
    #2
    1. Advertising

  3. On Sat, 22 Mar 2008 12:50:53 +0100, Tomasz bla Fortuna wrote:
    > Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT) Sanchit
    > <> napisał(a):
    >> I want to know how does printf (stdio library function) works? Does
    >> this depand on complier (I am using gcc on Linix)

    > Uhm. It depends on implementation (so on the library not the compiler
    > itself).


    It depends on both the library and the compiler. For example, the
    compiler may choose to transform
    (void) printf("Hello, world!\n");
    into
    (void) puts("Hello, world!");
    which behaves the same, but works differently.
    Harald van Dijk, Mar 22, 2008
    #3
  4. Sanchit

    Sanchit Guest

    On Mar 22, 4:50 pm, Tomasz bla Fortuna <> wrote:
    > Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
    > Sanchit <> napisa³(a):
    >
    > > I want to know how does printf (stdio library function) works? Does
    > > this depand on complier (I am using gcc on Linix)

    >
    > Uhm. It depends on implementation (so on the library not the compiler
    > itself).
    >
    > > Does it uses some buffer in which it stores all what needed to be
    > > printed and in end of program it prints that or something else.

    >
    > You'll best just grab the glibc source and find this implementation.
    >
    > --
    > Tomasz bla Fortuna
    > jid: bla(at)af.gliwice.pl
    > pgp: 0x90746E79 @ pgp.mit.edu
    > www:http://bla.thera.be
    >
    > signature.asc
    > 1KDownload


    I have done that already.. but there is not much description
    Sanchit, Mar 22, 2008
    #4
  5. In article <>,
    Sanchit <> wrote:

    >I want to know how does printf (stdio library function) works? Does
    >this depand on complier (I am using gcc on Linix)


    Mostly it depends on the C library implementation. The compiler
    itself may know about printf() (for example, so that it can warn about
    mismatches between the format and the arguments) but it doesn't have
    to.

    >Does it uses some buffer in which it stores all what needed to be
    >printed and in end of program it prints that or something else.


    printf() typically works by going through the format string, outputting
    the plain characters. When it comes to a % it gets the next argument
    using va_arg with a type depending on the format, and then converts it
    to characters and outputs it.

    The output is done by calling putc() on each character - or by some
    equivalent method - and that will do the usual buffering of
    characters. Usually for output to a terminal it will save up the
    characters until it's got a whole line, and for output to a file it
    will save them up until it's got a reasonable size block. If there
    are still any characters waiting in the buffer when the program ends,
    they are output then. Outputting each character individually would be
    slower. You can use the function setvbuf() to control the buffering.

    -- Richard



    --
    :wq
    Richard Tobin, Mar 22, 2008
    #5
  6. Sanchit

    Sanchit Guest

    On Mar 22, 5:21 pm, (Richard Tobin) wrote:
    > In article <>,
    >
    > Sanchit <> wrote:
    > >I want to know how does printf (stdio library function) works? Does
    > >this depand on complier (I am using gcc on Linix)

    >
    > Mostly it depends on the C library implementation. The compiler
    > itself may know about printf() (for example, so that it can warn about
    > mismatches between the format and the arguments) but it doesn't have
    > to.
    >
    > >Does it uses some buffer in which it stores all what needed to be
    > >printed and in end of program it prints that or something else.

    >
    > printf() typically works by going through the format string, outputting
    > the plain characters. When it comes to a % it gets the next argument
    > using va_arg with a type depending on the format, and then converts it
    > to characters and outputs it.
    >
    > The output is done by calling putc() on each character - or by some
    > equivalent method - and that will do the usual buffering of
    > characters. Usually for output to a terminal it will save up the
    > characters until it's got a whole line, and for output to a file it
    > will save them up until it's got a reasonable size block. If there
    > are still any characters waiting in the buffer when the program ends,
    > they are output then. Outputting each character individually would be
    > slower. You can use the function setvbuf() to control the buffering.
    >
    > -- Richard
    >
    > --
    > :wq


    can u please tell me a source where i can read this behaviour
    Sanchit, Mar 22, 2008
    #6
  7. Harald van Dijk schrieb:

    > (void) printf("Hello, world!\n");
    > into
    > (void) puts("Hello, world!");


    Uh - completely different question: the style of casting the return
    value (here: int) to void - is that common? Where is that taught?

    I taught some C-classes once and one of my students consistently used
    that style, which I found a little awkward. Why is this explicit cast
    needed (it isn't, IMHO)? Are there some weird compilers out there which
    omit a warning if the return value is discarded?

    Regards,
    Johannes

    --
    "PS: Ein Realname wäre nett. Ich selbst nutze nur keinen, weil mich die
    meisten hier bereits mit Namen kennen." -- Markus Gronotte aka Makus /
    Kosst Amojan / maqqusz / Mr. G / Ferdinand Simpson / Quartillia
    Rosenberg in dse <45608268$0$5719$-online.net>
    Johannes Bauer, Mar 22, 2008
    #7
  8. Johannes Bauer said:

    > Harald van D?k schrieb:
    >
    >> (void) printf("Hello, world!\n");
    >> into
    >> (void) puts("Hello, world!");

    >
    > Uh - completely different question: the style of casting the return
    > value (here: int) to void - is that common? Where is that taught?
    >
    > I taught some C-classes once and one of my students consistently used
    > that style, which I found a little awkward. Why is this explicit cast
    > needed (it isn't, IMHO)?


    Extra typing practice?

    > Are there some weird compilers out there which
    > omit a warning if the return value is discarded?


    No doubt - but then who's to say there isn't some weird compiler out there
    that warns about useless casts?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 22, 2008
    #8
  9. On Sat, 22 Mar 2008 15:04:21 +0100, Johannes Bauer wrote:
    > Harald van Dijk schrieb:
    >> (void) printf("Hello, world!\n");
    >> into
    >> (void) puts("Hello, world!");

    >
    > Uh - completely different question: the style of casting the return
    > value (here: int) to void - is that common? Where is that taught?
    >
    > I taught some C-classes once and one of my students consistently used
    > that style, which I found a little awkward. Why is this explicit cast
    > needed (it isn't, IMHO)? Are there some weird compilers out there which
    > omit a warning if the return value is discarded?


    Yes, there are some compilers that warn about ignored function return
    values. I know at least tendra will do that when you request extra
    warnings. But I deal with that by not requesting the warning, and I don't
    cast anything to void for it. :) The reason I included the casts is that
    an implementation may give printf("Hello, world!\n"); and
    puts("Hello, world!"); different return values, and if it documents this,
    the transformation from printf to puts would be incorrect when the return
    value is used. I wanted to make it clear that it is not used here.
    Harald van Dijk, Mar 22, 2008
    #9
  10. "Sanchit" <> wrote in message
    news:...
    >I want to know how does printf (stdio library function) works? Does
    > this depand on complier (I am using gcc on Linix)
    >
    > Does it uses some buffer in which it stores all what needed to be
    > printed and in end of program it prints that or something else.
    >

    Think how you'd write a cutdown sprintf(). Let it take only %d, %c and %s
    format specifiers.
    You'd have to set up a variable argument list, then step through the format
    string to determine what had been passed, then call va_arg with the right
    type to get the argument. Then you need to convert ints to human-readable
    ascii characters, and pass out to the buffer.

    printf() works in roughly the same way, though with lots of bells and
    twiddles.


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 22, 2008
    #10
  11. Sanchit

    Thad Smith Guest

    Sanchit wrote:
    > On Mar 22, 5:21 pm, (Richard Tobin) wrote:
    >> In article <>,
    >>
    >> Sanchit <> wrote:
    >>> I want to know how does printf (stdio library function) works? Does
    >>> this depand on complier (I am using gcc on Linix)

    >> Mostly it depends on the C library implementation. The compiler
    >> itself may know about printf() (for example, so that it can warn about
    >> mismatches between the format and the arguments) but it doesn't have
    >> to.
    >>
    >>> Does it uses some buffer in which it stores all what needed to be
    >>> printed and in end of program it prints that or something else.

    >> printf() typically works by going through the format string, outputting
    >> the plain characters. When it comes to a % it gets the next argument
    >> using va_arg with a type depending on the format, and then converts it
    >> to characters and outputs it.

    <snip>
    > can u please tell me a source where i can read this behaviour


    <OT>
    Formal capitalization and spelling are encouraged on Usenet, especially in
    the technical groups.
    </OT>

    The C Standard describes what the compiler and library must do to be a
    conforming implementation. Implementation details vary, though, from one
    to the next. I don't know of a source for high-level design for a
    particular implementation. While source code is available for different
    implementations, most don't provide a simple overview.

    --
    Thad
    Thad Smith, Mar 22, 2008
    #11
  12. Harald van Dijk <> writes:
    > On Sat, 22 Mar 2008 15:04:21 +0100, Johannes Bauer wrote:
    >> Harald van Dijk schrieb:
    >>> (void) printf("Hello, world!\n");
    >>> into
    >>> (void) puts("Hello, world!");

    >>
    >> Uh - completely different question: the style of casting the return
    >> value (here: int) to void - is that common? Where is that taught?
    >>
    >> I taught some C-classes once and one of my students consistently used
    >> that style, which I found a little awkward. Why is this explicit cast
    >> needed (it isn't, IMHO)? Are there some weird compilers out there which
    >> omit a warning if the return value is discarded?

    >
    > [...] The reason I included the casts is that an implementation may
    > give printf("Hello, world!\n"); and puts("Hello, world!"); different
    > return values, and if it documents this, the transformation from
    > printf to puts would be incorrect when the return value is used. I
    > wanted to make it clear that it is not used here.


    IMHO the fact that there was a semicolon on the end of each call was
    sufficient to indicate that the result was discarded. (Conceivably
    you could have hidden ``int result ='' on the previous line that you
    didn't show us, but I'd be willing to assume that you didn't.)

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 22, 2008
    #12
  13. Johannes Bauer <> writes:
    > Harald van Dijk schrieb:
    >> (void) printf("Hello, world!\n");
    >> into
    >> (void) puts("Hello, world!");

    >
    > Uh - completely different question: the style of casting the return
    > value (here: int) to void - is that common? Where is that taught?
    >
    > I taught some C-classes once and one of my students consistently used
    > that style, which I found a little awkward. Why is this explicit cast
    > needed (it isn't, IMHO)? Are there some weird compilers out there
    > which omit a warning if the return value is discarded?


    Both printf() and puts() return meaningful results. This code:

    printf("Hello, world!\n");
    puts("Hello, world!\n");

    silently discards those results. Some might argue that, if you're
    going to discard a result, it's better style to make it explicit.

    I think most C programmers (myself included) feel that using the
    function call as an expression statement makes that clear enough, and
    that the cast is just clutter.

    On the other hand, printf and puts *do* return meaningful results, and
    I think we're often too quick to ignore that fact.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 22, 2008
    #13
  14. Richard Heathfield wrote:
    > Johannes Bauer said:
    >
    >> Are there some weird compilers out there which
    >> omit a warning if the return value is discarded?

    >
    > No doubt - but then who's to say there isn't some weird compiler out there
    > that warns about useless casts?


    Both have existed; I once had the joy of writing a program which was
    supposed to compile without warnings on one of each ...
    J. J. Farrell, Mar 22, 2008
    #14
  15. Harald van Dijk schrieb:

    > The reason I included the casts is that
    > an implementation may give printf("Hello, world!\n"); and
    > puts("Hello, world!"); different return values, and if it documents this,
    > the transformation from printf to puts would be incorrect when the return
    > value is used. I wanted to make it clear that it is not used here.


    As Richard stated, I also think the ";" denoting the end of the
    statement is clear enough for the compiler to know that there is no
    lvalue present (i.e. the return is discarded).

    But the transformation from printf to puts - although I know that gcc
    does it - is it legal at all, according to the standard? I could preload
    some library which overrides printf() - it would never get called if the
    compiler subsituted the call by a puts().

    Regards,
    Johannes

    --
    "PS: Ein Realname wäre nett. Ich selbst nutze nur keinen, weil mich die
    meisten hier bereits mit Namen kennen." -- Markus Gronotte aka Makus /
    Kosst Amojan / maqqusz / Mr. G / Ferdinand Simpson / Quartillia
    Rosenberg in dse <45608268$0$5719$-online.net>
    Johannes Bauer, Mar 22, 2008
    #15
  16. On Sat, 22 Mar 2008 21:15:54 +0100, Johannes Bauer wrote:
    > Harald van Dijk schrieb:
    >> The reason I included the casts is that an implementation may give
    >> printf("Hello, world!\n"); and puts("Hello, world!"); different return
    >> values, and if it documents this, the transformation from printf to
    >> puts would be incorrect when the return value is used. I wanted to make
    >> it clear that it is not used here.

    >
    > As Richard stated, I also think the ";" denoting the end of the
    > statement is clear enough for the compiler to know that there is no
    > lvalue present (i.e. the return is discarded).


    That was Keith Thompson, and I was trying to make it more explicit for
    the readers of this group, not for the compiler. As I mentioned, I
    wouldn't have used the cast in actual code.

    > But the transformation from printf to puts - although I know that gcc
    > does it - is it legal at all, according to the standard?


    Yes.

    > I could preload
    > some library which overrides printf() - it would never get called if the
    > compiler subsituted the call by a puts().


    If you do that, as far as the standard is concerned, the behaviour is
    undefined, so there are no requirements on any particular behaviour by
    any implementation.
    Harald van Dijk, Mar 22, 2008
    #16
  17. Sanchit

    CBFalconer Guest

    Harald van D?k wrote:
    >

    .... snip about (void) on function calls ...
    >
    > The reason I included the casts is that an implementation may give
    > printf("Hello, world!\n"); and puts("Hello, world!"); different
    > return values, and if it documents this, the transformation from
    > printf to puts would be incorrect when the return value is used.
    > I wanted to make it clear that it is not used here.


    If the compiler wants to substitute puts for printf, it should
    detect that the return value is used and not do so. Otherwise it
    has a serious bug.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 22, 2008
    #17
  18. Sanchit

    CBFalconer Guest

    Johannes Bauer wrote:
    >

    .... snip ...
    >
    > But the transformation from printf to puts - although I know that
    > gcc does it - is it legal at all, according to the standard? I
    > could preload some library which overrides printf() - it would
    > never get called if the compiler subsituted the call by a puts().


    No you can't. So doing involved undefined behaviour. See the
    standard. The names of standard functions are reserved.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 22, 2008
    #18
  19. Sanchit

    CBFalconer Guest

    Sanchit wrote:
    >

    .... snip ...
    >
    > can u please tell me a source where i can read this behaviour


    u hasn't posted here for some years.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 22, 2008
    #19
  20. Sanchit

    Amandil Guest

    On Mar 22, 6:37 pm, CBFalconer <> wrote:
    > Sanchit wrote:
    >
    > ... snip ...
    >
    > > can u please tell me a source where i can read this behaviour

    >
    > u hasn't posted here for some years.
    >
    > [mail]: Chuck F (cbfalconer at maineline dot net)


    That piece of nastiness was totally uncalled for, as a polite response
    would have the same effect. Your English isn't that much better ("So
    doing...)... I believe several of the regulars have plonk()'ed you for
    your mostly useless noise. I only wish I knew how to do that with
    google...

    On a slightly (but only a very slightly) more serious note, printf is
    probably implemented as a wrapper:
    int printf(char *format, ...)
    {
    int i;
    va_list v;
    va_start(v, format);
    i = vfprintf(stdin, format, v);
    va_end(v);
    return v;
    }

    As for a 'real' implementation - one that actually does the work of
    stepping through the format string - K&R2 has a basic version that
    only recognizes %i and %s in one of the chapters. The method was
    mentioned earlier, no need to repeat it.

    On another note, printf() and puts() cannot be exchanged: the former
    returns the number of characters printed, the latter returns "a
    nonnegative value" (7.19.7.10.3).

    Regarding casting the returns from printf() to void: Some of my C
    texts mentioned them as needed by some C error-catchers, such as lint.
    I personally find they get in the way of reading code.

    While signing off, I'd like to reference something I posted in my very
    first post on clc: Easter this year is not two and a half weeks later
    than last year. Next year, however, it occurs on April 12, which is
    almost three weeks later than this year. This is caused by the
    addition of a whole month in the lunar calendar. The exact placement
    of this month is obviously not the same by all, as I conclude with...

    Happy Purim.
    -- Marty (not such a newbie anymore, and fully capable of starting a
    flame war...)
    Amandil, Mar 23, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    610
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,236
  3. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    430
  4. abargaddon
    Replies:
    1
    Views:
    192
    clintmazur
    Feb 4, 2008
  5. guru
    Replies:
    8
    Views:
    275
Loading...

Share This Page