how #ifndef in header file works for multiple inclusion?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As per my understanding first time when the preprocessor includes this header in some .c file it checks if __MY_HEADER_ is defined, since its not defined it will include the following code into the .c file along with defining __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.
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.
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)?
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
#define THIS_H
#include "that.h"
#include "theOther.h"
//... Other declarations, etc. here
#endif
And in that.h, you could have
Code:
#ifndef THAT_H
#define THAT_H
#include "this.h"
#include "theOther.h"
//... Other declarations, etc. here
#endif
Notice that without the conditionals, there would be a circular reference (this refers to that, which refers to this...), which would be a Bad Thing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.