Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for printf function in C [duplicate]

Tags:

c

printf

stdio

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.

like image 314
Rainer Zufall Avatar asked Feb 01 '11 19:02

Rainer Zufall


People also ask

How do I printf a double?

We can print the double value using both %f and %lf format specifier because printf treats both float and double are same. So, we can use both %f and %lf to print a double value.

Can you multiply printf in C?

No, you can't do this in 'C' as far as I think.In python multiplication of a string by a number is defined as 'repetition' of the string that number of times. One way you can do this in 'C++'(a superset of 'C' language) is through 'operator overloading'.

What is printf () in C?

"printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (lexing aka. parsing).


1 Answers

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

like image 71
mschaef Avatar answered Sep 28 '22 06:09

mschaef