2

What I want to know is how a compiler and program work.

For example, in 'Hello, world!' example with, say, hello.c, it goes like this as everybody knows: (using GNU gcc)

$ gcc -o hello hello.c
$ ./hello

Hello world!

I just got a question how printf, one of the easiest and familiar function, is defined or used.

To find a answer by myself, I found whole header files included in stdio.h, and included in included one, included in included in included one. There are almost 80 header files included in stdio.h. And I look for every single file whether it contain the word 'printf'. There were 3 header files.

stdio.h (itself)

bits/stdio2.h

bits/stdio-ldbl.h

I don't know about pre-processor syntax fully, but I'm quite sure that the text in those files are not enough to define the printf function. For example, in stdio.h, printf is roughly referred like this:

...
namespace std{
...
extern int printf (const char *__restrict__format, ...);
...
}
...

I know it says syntax and kind of declaration, but I think it is not a definition or building of printf.

So I think that there is something deep inside to answer my question, and I hope some of you have one.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Parker
  • 23
  • 5
  • 1
    I don't quite see a question here. Are you asking where the definition is? It's in a library, which you may or may not have the source for; you can see it [here](https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=stdio-common/printf.c). – Mike Seymour Nov 04 '14 at 14:14
  • 4
    If you are looking for the source code for `printf`, your question is a duplicate of this one: http://stackoverflow.com/questions/4867229/code-for-printf-function-in-c Otherwise I am not sure I understand what exactly you are looking for. The "principal" (principle?) of printf? What do you mean by that? – jogojapan Nov 04 '14 at 14:16

3 Answers3

5

You are correct, the declaration found in the header file is a forward declaration only.

This contains the function signature, which is everything the compiler needs to call the function. Calling convention, type of each argument (so it can perform implicit casting to that type), etc.

The actual definition isn't involved in the compile process at all. Rather it is provided by the C standard library, called something like libc.a or msvcrt.lib. The linker does the work of making sure function calls in your code, turned into CALL instructions by the compiler, use the correct address of the library function implementation.

To see the source code for the implementation, you'd need to find out what runtime library you're using and download its source code. Paid editions of Visual C++ come with CRT source code, which is the runtime also used by the mingw edition of gcc compilers for Windows. On Linux, you want to download the glibc source code. For cygwin, check out newlib. Embedded devices often use uclibc For some exotic environments, the C library may be closed source, although this can really get in the way of debugging, so most of the time source code is available even if the license is restrictive.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Let us limit to C99 specifcally (not C++).

printf is a variadic function. You can define your own variadic functions with stdarg(3).

printf is also a standard function in hosted C99. When you have an #include<stdio.h> the compiler is allowed to optimize it specifically. And GCC is doing that. See this answer.

BTW, the C standard also defines vprintf(3).

At last, there exist free software implementations of standard libc. Notably GNU glibc and musl-libc. You could study their implementation of printf (for musl-libc: see file src/stdio/printf.c then src/stdio/vfprintf.c)

You might study the generated assembler code hello.s (look inside it with some editor or pager) obtained by

 gcc -Wall -O -fverbose-asm -S hello.c
Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Header files usually contains the functio9n declaration, not the definition. The definition of the function will be placed in the standard C library [for example, glibc in case of linux] for your plarform. You have to download and analyze the source code for that.

For your reference, you can refer to the source code of printf in arch/x86/boot/printf.cinside kernel code [3.17]. to get the idea behind the implementation logic of printf().

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261