C (header file) question
Results 1 to 6 of 6

Thread: C (header file) question

  1. #1

    C (header file) question

    When I learned C in college all we were told about header file was to include them if useing a function that needs it.

    I did not like that answer, so I investigated, I opened stdio.h to see what it had inside, I found alot of pre-processor directives and function prototypes, and some defined constands and structures.

    My question:

    If printf() is declared (function prototype) inside stdio.h
    Where is the definition?

    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?
    test

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    339

    Re: C (header file) question

    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>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  3. #3
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,535

    Talking tadaaa

    you can give the compiler a command to stop after pre-processing..
    then you can see what it does before realy compiling the thing..

    This I got from the CC Man pages (man cc)
    -E Stop after the preprocessing stage; do not run the compiler proper. The output is preprocessed source code, which is sent to the standard output.

    So that would make.

    cc -E (filenames.c/.h) > tempfile.txt

    to store it in a temporary file..
    or

    cc -E (filenames.c/.h) | less

    to view..


    some good reading material on the pre-processor : http://www.cs.bath.ac.uk/~pjw/NOTES/...eprocessor.pdf
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  4. #4
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,210
    jdenny's answer is right on. Basically, the actual code for the function is pre-compiled in an external library which is made available when you include stdio.h. Generally, the pre-processor directives and function/procedure prototypes are enough to let you know what you need to do to use the function. If you are looking for some more detail, I suggest checking out some programming web sites. It's unlikely, however, that you will find any code for the printf function or similar functions, since it's machine dependent (every compiler has a different stdio.h depending which OS it's designed for) and, as far as I know, is written using assembly calls...

    AJ

  5. #5
    Thank you everyone, It had been a very long time that question had remained strolling around my brain cells. It is nice to finaly know.
    test

  6. #6
    I went to Bloodshed's forum and asked the developer's themselves the question you asked. The response was:

    search google for "gcc stdlib source code" or goto http://www.stlport.org/ I doubt it will be of much use... the source code for both seems to have been organized by a blind man with tail, and even when you find it printf the comments explaining it are extremely cryptic... as in the guy who wrote them could easily get a job writting windows error messages.

    Zero Valintine
    Hope this helps.

    I downloaded the source code, but it does me no good when I do not know C++ very well. I'll keep it on hand so I can view it later when I do understand classes and more functions... that was a good question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •