How #include statements work in C?

Programming, for all ages and all languages.
Post Reply
kim

How #include statements work in C?

Post 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..
proxy

Re:How #include statements work in C?

Post 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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How #include statements work in C?

Post 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.
Post Reply