Regarding multiple inclusion of files

Discussion in 'C Programming' started by ramsatishv@gmail.com, Dec 6, 2007.

  1. Guest

    Hi,

    If I include a ".h" file for multiple times, will it increase my
    program size??

    Regards
    Ram.
     
    , Dec 6, 2007
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > If I include a ".h" file for multiple times, will it increase my
    > program size??

    Unless you do things in these headers that should be done there, like
    defining variable rather than just declaring them, no.
    However, it's a good idea to prevent that from happening using header guards

    #ifndef myheader_h
    #define myheader_h
    /* your header here */
    ....
    #endif /* myheader_h */


    Bye, Jojo
     
    Joachim Schmitz, Dec 6, 2007
    #2
    1. Advertising

  3. Ben Pfaff Guest

    writes:

    > If I include a ".h" file for multiple times, will it increase my
    > program size??


    Normally, no. It might take longer to compile, though.
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Dec 6, 2007
    #3
  4. Joachim Schmitz wrote:
    > <> schrieb im Newsbeitrag
    > news:...
    >> Hi,
    >>
    >> If I include a ".h" file for multiple times, will it increase my
    >> program size??


    > Unless you do things in these headers that should be done there, like
    > defining variable rather than just declaring them, no.


    I think you mean "shouldn't be done". Also, if you define a variable in
    a header and include it multiple times, it won't increase your code size
    because your code will no longer compile...

    > However, it's a good idea to prevent that from happening using header guards
    >
    > #ifndef myheader_h
    > #define myheader_h
    > /* your header here */
    > ...
    > #endif /* myheader_h */


    Quite.
     
    Philip Potter, Dec 6, 2007
    #4
  5. "Philip Potter" <> schrieb im Newsbeitrag
    news:fj9erg$d07$...
    > Joachim Schmitz wrote:
    >> <> schrieb im Newsbeitrag
    >> news:...
    >>> Hi,
    >>>
    >>> If I include a ".h" file for multiple times, will it increase my
    >>> program size??

    >
    >> Unless you do things in these headers that should be done there, like
    >> defining variable rather than just declaring them, no.

    >
    > I think you mean "shouldn't be done".

    Indeed. Fingers faster than brain, yet again...

    > Also, if you define a variable in a header and include it multiple times,
    > it won't increase your code size because your code will no longer
    > compile...

    Unless it is included in everal modules

    Bye, Jojo
     
    Joachim Schmitz, Dec 6, 2007
    #5
  6. "Philip Potter" <> wrote in message
    news:fj9erg$d07$...
    > Joachim Schmitz wrote:
    >> Unless you do things in these headers that should be done there, like
    >> defining variable rather than just declaring them, no.

    >
    > I think you mean "shouldn't be done". Also, if you define a variable in a
    > header and include it multiple times, it won't increase your code size
    > because your code will no longer compile...
    >

    You can make a variable static, so each includer has his own local copy.

    This is seldom a good idea.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Dec 6, 2007
    #6
  7. Chris Dollin Guest

    wrote:

    > If I include a ".h" file for multiple times, will it increase my
    > program size??


    Multiple times in the same compilation unit, or in different compilation
    units?

    It depends on the .h file. Typically not, for well-written .h files, but
    of course it's possible to arrange otherwise:

    fragment.h:

    { 17, 42 },

    repeating.c:

    static int silly[][2] =
    {
    #include "fragment.h"
    #include "fragment.h"
    #include "fragment.h"
    #include "fragment.h"
    #include "fragment.h"
    #include "fragment.h"
    };

    Something like this can even be useful -- `fragment` can depend on a macro
    which is #define'd and #undef'd and re-#define'd in `repeating`. But this
    happens sufficiently infrequently that you do a sanity check if you come
    across it (ie there may well be a better design).

    --
    Chris "still crazy after all these gears" Dollin

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
     
    Chris Dollin, Dec 7, 2007
    #7
  8. In article <fjb89d$4gj$>,
    Chris Dollin <> wrote:
    >It depends on the .h file. Typically not, for well-written .h files, but
    >of course it's possible to arrange otherwise:
    >
    >fragment.h:
    >
    > { 17, 42 },
    >
    >repeating.c:
    >
    > static int silly[][2] =
    > {
    > #include "fragment.h"
    > #include "fragment.h"
    > #include "fragment.h"
    > #include "fragment.h"
    > #include "fragment.h"
    > #include "fragment.h"
    > };
    >
    >Something like this can even be useful -- `fragment` can depend on a macro
    >which is #define'd and #undef'd and re-#define'd in `repeating`. But this
    >happens sufficiently infrequently that you do a sanity check if you come
    >across it (ie there may well be a better design).


    If you use something like this, I recommend not using the suffix ".h"
    for the filename. Keep ".h" for real *h*eader files - that is, files
    of declarations rather than miscellaneous bits of C.

    -- Richard

    --
    :wq
     
    Richard Tobin, Dec 7, 2007
    #8
  9. Guest

    On Dec 7, 5:33 pm, (Richard Tobin) wrote:
    > In article <fjb89d$>,
    > Chris Dollin <> wrote:
    >
    >
    >
    >
    >
    > >It depends on the .h file. Typically not, for well-written .h files, but
    > >of course it's possible to arrange otherwise:

    >
    > >fragment.h:

    >
    > > { 17, 42 },

    >
    > >repeating.c:

    >
    > > static int silly[][2] =
    > > {
    > > #include "fragment.h"
    > > #include "fragment.h"
    > > #include "fragment.h"
    > > #include "fragment.h"
    > > #include "fragment.h"
    > > #include "fragment.h"
    > > };

    >
    > >Something like this can even be useful -- `fragment` can depend on a macro
    > >which is #define'd and #undef'd and re-#define'd in `repeating`. But this
    > >happens sufficiently infrequently that you do a sanity check if you come
    > >across it (ie there may well be a better design).

    >
    > If you use something like this, I recommend not using the suffix ".h"
    > for the filename. Keep ".h" for real *h*eader files - that is, files
    > of declarations rather than miscellaneous bits of C.
    >
    > -- Richard
    >
    > --
    > :wq- Hide quoted text -
    >
    > - Show quoted text -


    Hi,

    Thanks for youre replies. MY situation is something like this:

    test.h
    --------
    #define MACRO 1
    void test();
    extern int i;

    test.c
    -------
    #include "test.h"
    #include "test.h"

    int i = 23;

    void main()
    {
    printf("test..");
    }

    In this case, whether program size increases??

    Regards
    Satish.
     
    , Dec 7, 2007
    #9
  10. <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > Thanks for youre replies. MY situation is something like this:
    >
    > test.h
    > --------
    > #define MACRO 1
    > void test();
    > extern int i;
    >
    > test.c
    > -------
    > #include "test.h"
    > #include "test.h"
    >
    > int i = 23;
    >
    > void main()
    > {
    > printf("test..");
    > }
    >
    > In this case, whether program size increases??

    No

    Bye, Jojo
     
    Joachim Schmitz, Dec 7, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fritz Foetzl
    Replies:
    14
    Views:
    581
    Jonathan Turkanis
    Jan 20, 2004
  2. Replies:
    4
    Views:
    958
    M.E.Farmer
    Feb 13, 2005
  3. David T. Ashley

    Multiple Inclusion of Standard Header Files

    David T. Ashley, Jan 14, 2007, in forum: C Programming
    Replies:
    7
    Views:
    364
    Serve Laurijssen
    Jan 14, 2007
  4. Replies:
    11
    Views:
    1,239
    Ian Collins
    Aug 5, 2006
  5. Peter Flynn
    Replies:
    1
    Views:
    533
    Peter Flynn
    Jul 27, 2008
Loading...

Share This Page