Re: C (header file) question
Quote:
I can cal printf:
printf("\aHello world!\n");
and #include <stdio.h> tells the preprocessor to look in the proper directory for stdio.h and put that code into my code, but that only gives me the prototype, if I were to examine my code after the preprocessor but before it is compiled, I would see alot of stuff added, but the inportant thing in my case is the prototype for printf and the definition,
Where is printf defined?
<I think...>
You're right. Header files contain pre-processor directives and function prototypes, some defined constands and structures and some other definition. So where is a (public) function like printf defined? Before continuing...
Strictly speaking, the term "compiling" actually consists of two different processes. The first one is the compiling itself, which basically scanning and preprocessing the source code, and then converting it into an temporary object file (in UNIX it's .o file, in DOS/Windows it's .obj file). After that, the second process, linking, will link the generated object file to the C's public object files (generally called "library"), and then generate the final executable code. The temporary object file is then deleted at this time.
Back to the original question, the printf function body is defined in one of the C's library files, in a compiled format. The printf function prototype, as you found out, is defined in a header file (stdio.h). During the linking process, the linker will identify which function are "external", and try to resolve it by referring to its associated library, and include it in the exec code.
Some external functions are not linked statically during compile time, they are resolved dynamically when necessary during run time, by loading the so-called dynamic-link library (in DOS/Windows they're .dll files).
</I think...>
Note: Some C compilers don't even care whether you "include <stdio.h>" or not. Some just give you "printf() function is not defined" warning during compile time, but continue generate the exec file. The function prototypes in header files are used to check the validity of function argument count and types and such.
Note: In UNIX, use nm to locate which libraries contain a given function. Here's a sample of nm output on my Cygwin prompt (I have to boot my other partition to get Linux running, but I'm sure the result is similar):
$ cd /usr/lib
$ nm -o lib*.a | grep -w __printf
libc.a:ds00224.o:00000000 T __printf
libcygwin.a:d000352.o:00000000 T __printf
libg.a:d000352.o:00000000 T __printf
Have fun!
Peace always,
<jdenny>