Regarding multiple inclusion of files

R

ramsatishv

Hi,

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

Regards
Ram.
 
J

Joachim Schmitz

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
 
P

Philip Potter

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.
 
J

Joachim Schmitz

Philip Potter said:
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
 
M

Malcolm McLean

Philip Potter said:
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.
 
C

Chris Dollin

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).
 
R

Richard Tobin

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
 
R

ramsatishv

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

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.
 
J

Joachim Schmitz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top