I did not understand the functioning of #ifndef ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I did not understand the functioning of #ifndef ?

I did not understand the functioning of #ifndef in infinite inclusions. Someone will be able to explain to me more (where it is necessary to put the # of the two files to avoid inclusion for example).

4th Jun 2017, 12:45 AM
Soussi Mohamed
Soussi Mohamed - avatar
1 Answer
+ 12
The # is to indicate preprocessor commands, in order to help create the proper environment for the program. I the specific case of circular inclusion, you can create your own flags to indicate that you've already got what you need. file1.h: #define file1 #include <stdio.h> #include file3.h <header definitions here> file2.h: #include file1.h <more header stuff> file3.h: #ifndef file3 #define file3 #include file1.h #include <stdlib.h> more useful declarations... #endif mainfile.cpp: #include file1.h #include file2.h #include file3.h The #ifndef statement protects you from going back and forth between the header 1 and 3 files, while allowing you to just list out the known dependencies without having to know and account for the circular references in the actual code files. It's a good defensive technique, and something to keep in mind as your get into more complicated projects. Did that clarify anything?
4th Jun 2017, 2:24 PM
Jim
Jim - avatar