Header question

This is a discussion on Header question within the C Programming forums, part of the General Programming Boards category; Allos all, I have a question about header files. Ive been reading and working through a couple books now and ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    11

    Header question

    Allos all, I have a question about header files.

    Ive been reading and working through a couple books now and I hit a chapter about makeing my own header files. This got me intrested in exactly what was in the stdio.h and stdlib and conio.h headers I've been useing since almost day one. But when I go into them to check out what the functions are that I keep calling like printf, gets, getche ect.. ect.. There just variable declarations or what looks like function prototypes yet there is no function or any other refrence to them.

    Am i missing something? If I wanted to use gets() in a program wouldnt it be better to just use char* gets(char*); instead of including the whole stdlib.h when I dont need anything else in it? or is there more going on then im picking up on?

    The book dosnt really cover this and I looked but didnt find what i was looking for in the search on the boards.

    Thanks
    ~Meloshski

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You are missing libraries. When you compile the standard hello world, you include references to printf, but when the compiler hits those prototypes, it can't find them, so it just leaves those prototypes and leaves it to the linker to handle. When the linker ifnds them, it automatically includes the library called libc. The libc libarary is really just compiled .c called .so or .dll. It finds the function in there and puts it in the final executable.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    With most (good) compilers, it will automatically efficienize your code to only use the functions that you use in your code, so your exe isn't bloated just by including a .h file.

    So in other words, don't make your own prototypes to the functions, it is much better to just include the .h file and have your compiler worry about it.

    -Good luck with your C learning experience!

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Meloshski
    Allos all, I have a question about header files.
    What must be understood here is that a header is an interface. Let's try to make it clear.

    The C language being 'function oriented' (aka prodcedural), the main entity is the function (isn't the main() function the entry point of any C program). Hence, the code is generally organized into functions calling other functions.

    When you write a function, you use a 'function header'
    Code:
    <return type> <function name> ([<parameters>])
    and a function body
    Code:
    {
       <locals, instructions etc.>
    }
    The function header actually describes the interface of the function.
    • How to call it (the name)
    • What the entry parameters are (type, number, order)
    • What is returned

    This header can be detached from the function an put in some file called a 'header file' (with the .h extension).

    Hence you have, on one hand, the header file with the separated functions headers (aka 'prototypes' in modern C, the 'declaration' style being deprecated),
    Code:
    <return type> <function name> ([<parameters>]);
    and, on the other hand, the implementation files (.c or whatever, could be assembly or another language) that hold the bodies of the functions, aka 'definition' or 'implementation'.
    Code:
    <return type> <function name> ([<parameters>])
    {
       <locals, instructions etc.>
    }
    Last edited by Emmanuel Delaha; 07-04-2004 at 07:58 AM. Reason: Wording
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    Thanks Emmanuel that clears up my confusion about what I was seeing. And why I couldnt find the actuall function that did the work.

    ~Meloshski

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header File question
    By vishalag in forum C Programming
    Replies: 1
    Last Post: 06-17-2009, 08:25 AM
  2. Header files question
    By Programmer_P in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2009, 02:16 PM
  3. Linux header question
    By invisibleghost in forum Linux Programming
    Replies: 5
    Last Post: 02-17-2005, 10:03 AM
  4. Replies: 6
    Last Post: 04-02-2002, 05:46 AM
  5. header file question
    By unanimous in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 08:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21