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?

4
  • 2
    By "original form", do you mean the function's source code? Commented Dec 22, 2020 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. Commented Dec 22, 2020 at 17:07
  • You can take a look here github.com/Intersec/lib-common/blob/master/src/core/… but printf is not easy to jump in Commented Dec 22, 2020 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. Commented Dec 22, 2020 at 17:19

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.