0

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?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 2
    By "original form", do you mean the function's source code? – Shawn Dec 22 '20 at 17:04
  • 1
    As a beginner you won't learn much from looking at the source code of `printf`, it's a really complicated function. Google _printf source code gnu_ you you'll find plenty of implementations. For the source code of _your_ `printf` it might be provided somewhere, but without more information we can't tell much more. – Jabberwocky Dec 22 '20 at 17:07
  • You can take a look here https://github.com/Intersec/lib-common/blob/master/src/core/str-iprintf.c#L467 but printf is not easy to jump in – Ôrel Dec 22 '20 at 17:13
  • The Standrad defines: `int printf(const char *format, ...);` that's is your reference. Everything else is implementation details and should not be of prinary interest. – alk Dec 22 '20 at 17:19

2 Answers2

0

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 means printf works if you want to print a string with 0 variables and with x variables, etc.

Matteo Pinna
  • 409
  • 4
  • 9
0

What is extern and ... in this sentence? How and where can I see original form of printf function?

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, all printf variants, such as vprintf, fprintf etc eventually calls vfprintf. The function vfprintf 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 in glibc (one implementation of the standard library) here

https://github.com/bminor/glibc/blob/master/stdio-common/vfprintf-internal.c