473,563 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Mar 22 '08 #1
20 6826
Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT)
Sanchit <sa************ @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-----

Mar 22 '08 #2
On Sat, 22 Mar 2008 12:50:53 +0100, Tomasz bla Fortuna wrote:
Dnia Sat, 22 Mar 2008 04:35:24 -0700 (PDT) Sanchit
<sa************ @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.
Mar 22 '08 #3
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
Mar 22 '08 #4
In article <cb************ *************** *******@e10g200 0prf.googlegrou ps.com>,
Sanchit <sa************ @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
Mar 22 '08 #5
On Mar 22, 5:21 pm, ri*****@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <cb************ *************** *******@e10g200 0prf.googlegrou ps.com>,

Sanchit <sa************ @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
Mar 22 '08 #6
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 ...
Mar 22 '08 #7
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

Mar 22 '08 #8
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

Mar 22 '08 #9
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...)
Mar 23 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5922
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
8
2697
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
25
36468
by: Joakim Hove | last post by:
Hello, I have code which makses use of variables of type size_t. The code is originally developed on a 32 bit machine, but now it is run on both a 32 bit and a 64 bit machine. In the code have statements like this: size_t buffer_size; printf("Total buffer size: %ud bytes \n",buffer_size);
10
1919
by: bright116 | last post by:
It's strange! I wrote such fragment: char messageBody = ""; printf("message body: %s\n", messageBody); strcat(messageBody, "Signal= "); printf("message body: %s\n", messageBody); strcat(messageBody, CCDTMFMap); printf("message body: %s\n", messageBody); strcat(messageBody, CRLF); printf("message body: %s\n", messageBody);
6
5752
by: alternativa | last post by:
Hi, I have problem with the following function - it was intended to ask a user for a 4-digits number: double ask_for_number (void) { char *notint; char s2; double entered_number;
4
1580
by: amit | last post by:
Hi guys!I am trying to write a program which will segregate some selected keywords from a given file.The source code is given alongwith #include<stdio.h> #include<string.h> char key_set={"ami\0","inc\0","lud\0"}; #define MAX_KW_SIZE 10 #define MAX_OCUR 100
5
468
by: prabu | last post by:
Hi all, I want to know, what does the value of the array in the following statement mean and How to print the content of the array SMB_Negotiate using printf? unsigned char SMB_Negotiate = "\x00\x00\x00\x85\xFF\x53\x4D\x42\x72\x00\x00\x00\x00\x18\x53\xC8"
92
6135
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
45
9321
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error, ((time_t)-1) is returned, and errno is set
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7631
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7943
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3631
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
912
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.