Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Regarding multiple inclusion of files

Reply
Thread Tools

Regarding multiple inclusion of files

 
 
ramsatishv@gmail.com
Guest
Posts: n/a
 
      12-06-2007
Hi,

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

Regards
Ram.
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-06-2007
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      12-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      12-06-2007
Joachim Schmitz wrote:
> <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
>> 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.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-06-2007
"Philip Potter" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fj9erg$d07$(E-Mail Removed)...
> Joachim Schmitz wrote:
>> <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed)...
>>> 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


 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      12-06-2007

"Philip Potter" <(E-Mail Removed)> wrote in message
news:fj9erg$d07$(E-Mail Removed)...
> 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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-07-2007
(E-Mail Removed) 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

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      12-07-2007
In article <fjb89d$4gj$(E-Mail Removed)>,
Chris Dollin <(E-Mail Removed)> 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
 
Reply With Quote
 
ramsatishv@gmail.com
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 5:33 pm, (E-Mail Removed) (Richard Tobin) wrote:
> In article <fjb89d$(E-Mail Removed)>,
> Chris Dollin <(E-Mail Removed)> 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.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-07-2007

<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Problems with multiple multiple-level inclusion of DTDs Peter Flynn XML 1 07-27-2008 08:14 PM
Multiple Inclusion of Standard Header Files David T. Ashley C Programming 7 01-14-2007 08:15 PM
How to avoid multiple definition of a variable by multiple inclusion of a header file lars.uffmann@rwth-aachen.de C++ 11 08-05-2006 10:24 PM
tool to check multiple inclusion of header file techBoy C Programming 6 03-15-2006 10:49 AM
Multiple Inclusion Guards Work in MSVC++ .NET...right? Fritz Foetzl C++ 14 01-20-2004 04:21 AM



Advertisments