Page 1 of 1

How #include statements work in C?

Posted: Mon Jun 28, 2004 10:04 am
by kim
How #include statements work in C under the hood??
When I include some file in header, It show different result from including in source file..

I mean When I include some file in header, It shows error..
but include in source file, no error..

How it works?
Is it different from ASM Macro?
ASM Macro changed the content of source before assembling, I know..

Re:How #include statements work in C?

Posted: Mon Jun 28, 2004 11:39 am
by proxy
includes literally replace that line "#include file.h" with the contents of "file.h" at that exact spot.

somtimes when you include somthing is a factor because of different macros being defined.

proxy

Re:How #include statements work in C?

Posted: Mon Jun 28, 2004 1:25 pm
by Pype.Clicker
there are several 'tricks' in .H files that may make two inclusion of the same .H file produce different resulting source.

- "include-once" trick, using a preprocessor symbol to avoid re-inclusion is useful as soon as you want to avoid 'multiple definition' and can no longer keep track of what includes what:

Code: Select all

//--- myFile.h ---
#ifndef __MY_FILE
#define __MY_FILE
blablabla
#endif

//--- myFile.c ---
hehe
#include "myFile.h"
does it work ?
#include "myFile.h"
will not see the "blablabla" twice.

I suggest you try to use gcc -E to see the output of the preprocessor stage and see how it interracts with your sources.