Thread: What should be in a header file?

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    What should be in a header file?

    Hi,

    I just finished learning the basics of C programming from the deitel book. But it doesns't really explain how to use header files (.h files).

    What should be in a header file?

    When should it be they be used?

    How should they be used?

    What should they be used with? Like the ones in the standard library, providing links to other libraries when compiling a program etc.

    Any tutorials on the net and things like that?

    thnx

  2. #2
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70
    What should be in a header file?
    >>Predefined Macros; like CLOCKS_PER_SEC from <time.h>
    >>funtions prototypes and definitions; like abort() from <stdlib.h>
    |
    When should it be they be used?
    >>when u need to use one of it's function/macro for a task in your program:
    |
    <find Square root of a number>
    |
    #include <stdio.h> //for input and output funtion "printf();
    #include <math.h> //for mathematical ops "sqrt()"
    #include <stdlib.h> //for "abort()" exits the program.
    int main(int argc,char *argv[]){
    if(argc != 1){
    printf("You did not enter a value after the program's name\n");
    abort();
    }else{
    printf("the Square root of %f is: %f\n",argv[1],sqrt(argv[1])
    }
    return 0;
    }

    try :
    http://msdn.microsoft.com/library/de...ls/rc_0v8z.asp

    And try to read the FAQ please!!!
    Last edited by bljonk; 03-18-2002 at 11:23 AM.
    Ünicode¬>world = 10.0£

  3. #3
    Registered User compjinx's Avatar
    Join Date
    Aug 2001
    Posts
    214
    usually it hold definitions of stuff.
    look at it this way:
    one C file cannot "see" what is in another C file, not good if you want the first C file to use a function in the other C file, if you put a definition in an H file then the C file can look at the H file and say "oh, I see, theres a function declaration, I can use that function even though I can't see it"

    example:

    In First C file:

    ...Code...
    OutSideFunc();
    ...Code...

    In Second C File:

    OutSideFunc()
    {
    ...Code...
    }

    //now, this will give you an error because the first C function can't "see" the other file, so it can't tell if that function exists
    // now, if you declare it in a H file then:

    In H File:

    OutSideFunc();

    // then you include that into the first C file that file can "look" at the function declaration and say: "hmmm, the declaration is there, so it must be in another C File, it must exist so I won't worry about it.
    "The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
    Eric Porterfield.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    It's quite personal what you put in your headerfiles and what you don't.

    I normally put general definitions and macros in header files. So #define's and enum's and that kind of stuff are put in header files. Prototypes are put in C-files, a header-file is not necessary.

    File A:

    int function_A (int var);

    ..

    int function_A (int var)
    {
    ...
    }


    File B:

    extern int function_A (int var);

    int function_B (int var)
    {
    function_A (var);
    }

    Definitions which are not general, i.e. not used in more than 1 file, are put in the C-file.

    Just my point of view.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I like to put function prototypes, what they take, what they do, and what they return. Makes it simpler to go through and see what a function does.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Do i hav to include the file for the outside function? THen means i'll have multiple source files then right?

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    And although header files are included, it still need to link to the source of the functions. Where are the standard library functions located?

  8. #8
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    It differs~

    It differs from compiler to compiler;
    if u write your own "*.h" file to compile keep it in the same directory with the "*.c" file that contains the source code:
    e.g:

    "units.h"
    <stdio.h>

    int main(){
    ...
    }

    if you known where the Standard header files are stored you can save your own made header file there and use it like the standard ones in a program:
    e.g:

    <units.h>
    <stdio.h>

    int main(){
    ...
    }

    the compiler will look for the file unity.h in the normal place for the "*.h" files.

    if u happen to have time u can look for them in the compiler package for: djgpp, dm(digital mars), gcc & gnu I don't know about the other compilers. surf folders~
    Ünicode¬>world = 10.0£

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Do i hav to include the file for the outside function? THen means
    >i'll have multiple source files then right?

    Functions are defined in a source-file and you normally don't include sourcefiles. You should create a makefile in which you include the sourcefiles. By declaring a function as extern, the compiler knows the function is implemented somewhere else.

    Some environments offer the possibility to create a project in which you can add multiple source-files and header-files. Then the environment takes care of building.

  10. #10
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    yes i know the compiler knows the function exists somewhere else. But eventually it has to find it to compile the program. Welll where is it...?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    RE: External functions.

    Your compiler will basicly go through all the .c files in your project and compile them one at a time. When it gets to the end, it "links" all of them to form an executable. At this time, it ends up checking to make sure all of the external functions are there.

    When I say it checks to make sure they're there, it basicly goes through each file and looks for the included functions. If the needed function is in no file any place, then it spits out a warning or error at you.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70
    U have to search for them in the folders that came with your compiler package~

    [-]dm
    |
    [+]html
    [+]bin
    [+]src
    | ->*.h
    | ...
    [+]text
    [+]info

    surf folders~
    Ünicode¬>world = 10.0£

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Also, you said sources of external should not be included, only the header files need to be. SO how is the compiler going to find the source of the function and compile it if it's not included in the project?

    thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM