how #ifndef in header file works for multiple inclusion?
I have a header file with following contents,
Code:
#ifndef __MY_HEADER_ my doubt is, when I include this header in another .c file, how would it takes __MY_HEADER_ definition. According to me, as __MY_HEADER_ is already defined the contents of the header file should not be included in any further .c files. Please clarify me about this. |
No, the pre-processor does not know about __MY_HEADER_ in the other C-files; the pre-processor works on a single file (and the files that are included in that file) at a time.
|
Hi Wim Sturkenboom,
So you mean that the, "#ifndef __MY_HEADER_ #define __MY_HEADER_" is applied for every file freshly? I mean the macro __MY_HEADER_ is kind of local to each .c files which includes the header file "my_header.h" (the header file containing the above mentioned code snippet)? |
Yes, that's what I mean.
|
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
This construct is used when you have a lot interdependence between modules in a project. As an example, you may have a project with 3 C source files (this.c, that.c, theOther.c). Each C source file has a corresponding header file (this.h, that.h, theOther.h) which is #included in the .c source file.
In this.h, you could then have Code:
#ifndef THIS_H Code:
#ifndef THAT_H --- rod. |
All times are GMT -5. The time now is 12:54 PM. |