How does printf() works

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sanchit

    How does printf() works

    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.

  • Tomasz bla Fortuna

    #2
    Re: How does printf() works

    Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
    Sanchit <sanchitgupta.1 @gmail.comnapis a³(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.gliwi ce.pl
    pgp: 0x90746E79 @ pgp.mit.edu
    www: http://bla.thera.be

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

    iD8DBQFH5PKhT6w vGJB0bnkRAucXAJ 9MXo8HWUUBIAenK RAWW0FKCHrW5ACg nzXR
    2PyUKHQKgpmKh83 NlFQjHKM=
    =2kU+
    -----END PGP SIGNATURE-----

    Comment

    • Harald van =?UTF-8?b?RMSzaw==?=

      #3
      Re: How does printf() works

      On Sat, 22 Mar 2008 12:50:53 +0100, Tomasz bla Fortuna wrote:
      Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT) Sanchit
      <sanchitgupta.1 @gmail.comnapis ał(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.

      Comment

      • Sanchit

        #4
        Re: How does printf() works

        On Mar 22, 4:50 pm, Tomasz bla Fortuna <b...@thera.bew rote:
        Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
        Sanchit <sanchitgupt... @gmail.comnapis a³(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.gliwi ce.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

        Comment

        • Richard Tobin

          #5
          Re: How does printf() works

          In article <cbdde0a1-0a51-46cf-a671-dd590d918bad@e1 0g2000prf.googl egroups.com>,
          Sanchit <sanchitgupta.1 @gmail.comwrote :
          >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

          Comment

          • Sanchit

            #6
            Re: How does printf() works

            On Mar 22, 5:21 pm, richard@cogsci. ed.ac.uk (Richard Tobin) wrote:
            In article <cbdde0a1-0a51-46cf-a671-dd590d918bad@e1 0g2000prf.googl egroups.com>,
            >
            Sanchit <sanchitgupta.1 @gmail.comwrote :
            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

            Comment

            • J. J. Farrell

              #7
              Re: How does printf() works

              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 ...

              Comment

              • CBFalconer

                #8
                Re: How does printf() works

                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

                Comment

                • CBFalconer

                  #9
                  Re: How does printf() works

                  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

                  Comment

                  • Amandil

                    #10
                    Re: How does printf() works

                    On Mar 22, 6:37 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                    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...)

                    Comment

                    • CBFalconer

                      #11
                      Re: How does printf() works

                      Amandil wrote:
                      CBFalconer <cbfalco...@yah oo.comwrote:
                      >Sanchit wrote:
                      >>
                      >... snip ...
                      >>
                      >>can u please tell me a source where i can read this behaviour
                      >>
                      >u hasn't posted here for some years.
                      >
                      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...
                      What was impolite? What English was broken? The idea is to make
                      the OP (and others) realize that he is writing in incomprehensibl e
                      code and to correct his behaviour.

                      --
                      [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

                      Comment

                      • Sanchit

                        #12
                        Re: How does printf() works

                        See i have posted in a manner which can be understood. If somewhere my
                        English went wrong, that doesn't stopped anyone in understanding the
                        problem.

                        And I clearly stated the question. I wrote in second line

                        "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.
                        "

                        So i really wanted to know that if printf buffers or not. And if yes
                        then how.

                        Comment

                        • Richard Heathfield

                          #13
                          Re: How does printf() works

                          Amandil said:

                          <snip>
                          I only wish I knew how to [plonk] with google...
                          s/with// - i.e. stop using google, and get a real newsreader. (It's the
                          obvious step.)
                          >
                          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);
                          ITYM stdout, right?
                          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).
                          Well, that sounds easy to fix - implementations that wish to swap printf()s
                          of string literals with a puts() call can simply arrange that the
                          non-negative value puts() returns is the number of characters printed.
                          :-)

                          <snip>

                          --
                          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

                          Comment

                          • Amandil

                            #14
                            Re: How does printf() works

                            On Mar 23, 2:07 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                            Amandil said:
                            >
                            <snip>
                            >
                            I only wish I knew how to [plonk] with google...
                            >
                            s/with// - i.e. stop using google, and get a real newsreader. (It's the
                            obvious step.)
                            I know, but thus far I've been too cheap. Maybe soon. Some good
                            (free?) newsreaders were mentioned recently here, I started looking in
                            to them.

                            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);
                            >
                            ITYM stdout, right?
                            Of course, right as usual ;)
                            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).
                            >
                            Well, that sounds easy to fix - implementations that wish to swap printf()s
                            of string literals with a puts() call can simply arrange that the
                            non-negative value puts() returns is the number of characters printed.
                            :-)
                            >
                            Also make sure implementations swap only printf() of string literals
                            that ends in a '\n', because printf() does not add newline, where as
                            puts() does. I think there are enough differences - and the case where
                            there is none is extremely specific - that the implementation should
                            not take one function call and replace it with another. It could,
                            conceivably, but I wouldn't appreciate or justify it, unless puts() is
                            extremely faster.

                            -- Marty

                            Comment

                            • santosh

                              #15
                              Re: How does printf() works

                              Amandil wrote:

                              <snip>
                              Also make sure implementations swap only printf() of string literals
                              that ends in a '\n', because printf() does not add newline, where as
                              puts() does. I think there are enough differences - and the case where
                              there is none is extremely specific - that the implementation should
                              not take one function call and replace it with another. It could,
                              conceivably, but I wouldn't appreciate or justify it, unless puts() is
                              extremely faster.
                              As long as the "as if" rule is followed and there is no observable
                              difference in behaviour, the compiler is free to do whatever it wants
                              with your code. Or at least, I think so.

                              Comment

                              Working...