Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How does printf() works

Reply
Thread Tools

How does printf() works

 
 
Sanchit
Guest
Posts: n/a
 
      03-22-2008
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.

 
Reply With Quote
 
 
 
 
Tomasz bla Fortuna
Guest
Posts: n/a
 
      03-22-2008
Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
Sanchit <(E-Mail Removed)> 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)

iD8DBQFH5PKhT6wvGJB0bnkRAucXAJ9MXo8HWUUBIAenKRAWW0 FKCHrW5ACgnzXR
2PyUKHQKgpmKh83NlFQjHKM=
=2kU+
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      03-22-2008
On Sat, 22 Mar 2008 12:50:53 +0100, Tomasz bla Fortuna wrote:
> Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT) Sanchit
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Sanchit
Guest
Posts: n/a
 
      03-22-2008
On Mar 22, 4:50 pm, Tomasz bla Fortuna <(E-Mail Removed)> wrote:
> Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
> Sanchit <(E-Mail Removed)> 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
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      03-22-2008
In article <(E-Mail Removed)>,
Sanchit <(E-Mail Removed)> 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
 
Reply With Quote
 
Sanchit
Guest
Posts: n/a
 
      03-22-2008
On Mar 22, 5:21 pm, http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Tobin) wrote:
> In article <(E-Mail Removed)>,
>
> Sanchit <(E-Mail Removed)> 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
 
Reply With Quote
 
Johannes Bauer
Guest
Posts: n/a
 
      03-22-2008
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$(E-Mail Removed)-online.net>
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-22-2008
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
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      03-22-2008
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.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      03-22-2008

"Sanchit" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
In a template, Eval () does not works in fields ofcontrols, it works abargaddon ASP .Net Web Controls 1 02-04-2008 09:16 PM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
Webservice works once and then DOES NOT seem to work even though program does not crash Phi! ASP .Net Web Services 1 04-23-2004 08:42 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments