Code for printf function in C

CPrintfStdio

C Problem Overview


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

C Solutions


Solution 1 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRainer ZufallView Question on Stackoverflow
Solution 1 - CmschaefView Answer on Stackoverflow