including header files in the program file (.c) or in the header file (.h)

This is a discussion on including header files in the program file (.c) or in the header file (.h) within the C Programming forums, part of the General Programming Boards category; Hi, sorry for such a trivial question.... What is it better to include the header files in the header file ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    including header files in the program file (.c) or in the header file (.h)

    Hi,

    sorry for such a trivial question.... What is it better to include the header files in the header file or in the main program (probably bearing in mind that the .h file is not dependent on the header files to be included).

    Eg. main.c and main.h

    main.c
    Code:
    #include "main.h"
    #include <stdio.h>
    OR

    main.h
    Code:
    #include <stdio.h>
    main.c
    Code:
    #include "main.h"
    And if smn has a spare minute to provide a link or explain why one would be better than the other it would be great...

    Cheers

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    there is no standard way of doing that, nor is one any "better" than the other. A lot depends on how the .h is used -- is it is in only one *.c or *.cpp file or in many. If in may, then I'd add the other includes inside main.h. Are there definitions in main.h that depend on definitions in other includes? Yes, then I'd put those in main.h too.

    Also I include system header files, such as stdio.h, before any others that I may have created.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    ...
    #include "main.h"
    Last edited by Ancient Dragon; 12-14-2005 at 07:06 AM.

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    The coding standards that I have worked on differ on this. Some forbid "#include"s within header files, others allow them, but only if the definitions/declarations in the header file depends on something from those header files, and a third camp allows them (usually in order to make precompiled headers work better with some compiler suite or another.)

    That being said, I tend to go with the middle of the road camp -- header files may include other header files, but only if they are required by defs/decls within the header file. I believe that this helps prevent order of inclusion problems and keeps me from having to know the overall header file dependancy graph before I write a C or C++ file that depends on a given header file. The common practice of using
    Code:
    #ifndef THISHEADER_H_INCLUDED
    #define THISHEADER_H_INCLUDED
    ... (the rest of the header file)
    #endif /* THISHEADER_H_INCLUDED */
    makes this work, as the body of each header file is only parsed once.

    As they say, your mileage may (and will) vary. Offer not valid in Delaware and the US Virgin Islands. See other side of package for details.
    Insert obnoxious but pithy remark here

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    In my opinion, because it doesn't really matter most of the time, it's best, read as "less confusing", to only include headers in the file that they're needed. That way you don't have to wonder if one of the headers includes stdlib.h or it's an error when you see malloc, but not stdlib.h.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    Thanks everybody. You have been most helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  3. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 12:31 PM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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