I’m using GCC as compiler. I wanted to know original form of printf function. So I found stdio.h file in /usr/include(I’m using CentOS 8.2 in GCP).
When I opened stdio.h file and found printf, something was written like
extern int printf(const char *__restrict __format, ...);
What is extern and … in this sentence? How and where can I see original form of printf function?
2
Answers
extern
means that the function visibility is extended to the whole program. While for the...
it represent an unspecified number of arguments for the format specifiers%d - %ld etc.
. That meansprintf
works if you want to print a string with 0 variables and with x variables, etc.extern
means external linkage. It tells the linker this symbol should be found in one of the objects or libraries being linked.There are different implementations of
printf
. In most cases, allprintf
variants, such asvprintf
,fprintf
etc eventually callsvfprintf
. The functionvfprintf
itself is very complicated. Most implementations use a table-lookup method where a handler corresponding to one of the%
formatters is called to format the input.You can see the implementation of
vfprintf
inglibc
(one implementation of the standard library) herehttps://github.com/bminor/glibc/blob/master/stdio-common/vfprintf-internal.c