Head file redefinition

This is a discussion on Head file redefinition within the C Programming forums, part of the General Programming Boards category; Hi If there are three files. main.c and head2.h, head3.h. head3.h has been imported into head2.h already. And in main.c ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    Head file redefinition

    Hi

    If there are three files. main.c and head2.h, head3.h.
    head3.h has been imported into head2.h already. And in main.c
    Code:
    #include "head3.h"
    #include "head2.h"
    ................
    So head3.h has been imported twice in main.c(in main.c and in head2.h), do you think that variable defined in head3.h would cause some unexpected failure, and should I avoid such manner?
    Thanks.
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This multiple inclusion is a common problem, that's why some smart guy invented "inclusion guards". By using macros you prevent the contents in one file from being redefined if the file is included again:

    Code:
    #ifndef MYFILE_H
    #define MYFILE_H
    
    ... //The contents here
    
    #endif
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    The standard c99 dictates a maximum of 15 nested levels for include files.

    It would be a waste of resources to include the same file twice.

    If you look at your own header files in the header directory you'll
    more than likely notice that each header defines itself thus,

    Code:
    /*
        header1.h
    */
    #define _HEADER1_H_
    Knowing this we could now do something like,

    Code:
    /*
        header2.h
    */
    #define _HEADER2_H_
    
    #ifndef _HEADER1_H_
    #include "header1.h"
    #endif
    The preprocessor would then determine whether or not
    header1.h has already been included and proceed accordingly.

    Note that you shouldn't really use the leading underscore as
    the standard suggests that _[any] are reserved identifiers so
    HEADER1_H_ could be an alternative option.


    Hope that helps.

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, 10:39 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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