How does printf() works

S

Sanchit

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

Tomasz bla Fortuna

Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
Sanchit said:
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-----
 
H

Harald van Dijk

Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT) Sanchit

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

Sanchit

Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)


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


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
 
R

Richard Tobin

Sanchit said:
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
 
S

Sanchit

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.


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

can u please tell me a source where i can read this behaviour
 
J

Johannes Bauer

Harald said:
(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
 
R

Richard Heathfield

Johannes Bauer said:
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?
 
H

Harald van Dijk

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

Malcolm McLean

Sanchit said:
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.
 
T

Thad Smith

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

Keith Thompson

Harald van Dijk said:
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.)
 
K

Keith Thompson

Johannes Bauer said:
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.
 
J

J. J. Farrell

Richard said:
Johannes Bauer said:


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

Johannes Bauer

Harald said:
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
 
H

Harald van Dijk

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

CBFalconer

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

CBFalconer

Johannes said:
.... 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.
 
A

Amandil

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top