71

Possible Duplicate:
source code of c/c++ functions

I was wondering where I can find the C code that's used so that when I write printf("Hello World!"); in my C programm to know that it has to print that string to STDOUT. I looked in <stdio.h>, but there I could only find its prototype int printf(const char *format, ...), but not how it looks like internally.

Community
  • 1
  • 1
Rainer Zufall
  • 719
  • 1
  • 6
  • 3
  • From which implementation? Each platform has its own... – thkala Feb 01 '11 at 19:48
  • 1
    I don't think there's the source available. +1 anyway, I always wondered this. – BlackBear Feb 01 '11 at 19:49
  • Ultimately printf has to make an OS call so the exact implementation is platform specific. Following the trail from printf down to the actual OS call(s) can be pretty messy. The source for the C-Runtime Library (CRT) is usually available with the compiler (true for GCC and MSVC). – Tergiver Feb 01 '11 at 20:11
  • 1
    related - [Understanding the hardware of printf](http://stackoverflow.com/q/2457656/203667) & [C/C++ function definitions without assembly](http://stackoverflow.com/q/2442966/203667) – jschmier Feb 01 '11 at 20:57
  • 2
    `printf` is defined in terms of `putc` so there's no need for it to make any OS calls. – R.. GitHub STOP HELPING ICE Feb 01 '11 at 21:11
  • @R..: Typically, it's not possible to perform **any** I/O without making operating system calls. While you can talk directly to hardware or via BIOS, modern operating systems prevent you from doing so. – Tergiver Feb 02 '11 at 03:14
  • 1
    @Tergiver, @R: You're both right. `printf` handles formatting, then calls `putc`, which may call other helper functions but ultimately results in an OS call. – Ben Voigt Feb 02 '11 at 03:19
  • 1
    My point was that the OS calls are at a much lower level, and `printf` is essentially a pure library function on top of lower-level stdio calls. – R.. GitHub STOP HELPING ICE Feb 02 '11 at 03:21
  • 1
    Funny how this, a more specific question, triggering more specific answers, is higher-rated and perhaps more useful than the original question which caused this question to be "closed as a duplicate". – Dan Nissenbaum Mar 27 '13 at 12:29

1 Answers1

98

Here's the GNU version of printf... you can see it passing in stdout to vfprintf:

__printf (const char *format, ...)
{
   va_list arg;
   int done;

   va_start (arg, format);
   done = vfprintf (stdout, format, arg);
   va_end (arg);

   return done;
}

See here.

Here's a link to vfprintf... all the formatting 'magic' happens here.

The only thing that's truly 'different' about these functions is that they use varargs to get at arguments in a variable length argument list. Other than that, they're just traditional C. (This is in contrast to Pascal's printf equivalent, which is implemented with specific support in the compiler... at least it was back in the day.)

László Papp
  • 51,870
  • 39
  • 111
  • 135
mschaef
  • 1,620
  • 10
  • 12
  • Where is this file located under my linux system. I'm using RHEL. Thanks! – xxks-kkk May 11 '16 at 05:25
  • 1
    @Jerry Its object code is in your libc. Download the glibc source code for the source. – a3f Jul 28 '16 at 16:01
  • 5
    one of the most well-known library functions, and what's in [line 98 of its source code](http://sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/vfprintf.c;h=fc370e8cbc4e9652a2ed377b1c6f2324f15b1bf9;hb=3321010338384ecdc6633a8b032bb0ed6aa9b19a#l98)? bunch of `goto` and "This is a hack!!!" ;) – Cee McSharpface Jul 31 '18 at 20:42