+ 1

#ifndef

I can't understand why after this preprocessor command we need to use capital letters for example if my class headers name is myclass.h why I use #ifndef MYCLASS_H #define MYCLASS_H ?

22nd Oct 2017, 4:23 PM
Rasa Mirzaagasi
Rasa Mirzaagasi - avatar
7 Answers
+ 3
it's a convention to write everything in caps when using a macro, but you do not have to do it, you can write it in lowercase, it won't change anything
22nd Oct 2017, 8:28 PM
‎ɐısıօՏɐ
‎ɐısıօՏɐ - avatar
+ 3
and if you write it in uppercase you make sure it can't be confused with a variable or something
22nd Oct 2017, 8:30 PM
‎ɐısıօՏɐ
‎ɐısıօՏɐ - avatar
+ 3
The "#ifndef something" preprocessor directive tells the preprocessor to include the code between it and "#endif" only if "something" is not defined. To define "something", a "#define something" directive is used. Regarding the capital letters, — it's a convention to use all caps in the preprocessor definitions. And as for the subject of your question, you're asking about the header guards. They are used to avoid the mutiple inclusion of a header in the source. As you know, the "#include header" directive is substituted with the contents of the included header file. Consider an example: <iostream> contains "include <string>"; what would happen if we include iostream AND string in our source? If not for the header guards, string would've been included twice, and each deffinition inside it would be doubled, resulting in compile error. To prevent this, there is a practice to place header guards in each header file. They look like this: #ifndef TOKEN #define TOKEN // ... contents of our header ... #endif So, if TOKEN is defined, the preprocessor just deletes everything up to the #endif. If TOKEN is not defined, the contents will be left there, and the token will be defined (#define TOKEN). So, next time, the token will be defined, hence the header-guarded content will be included only once. As for what should be written as TOKEN, there is a convention to write a capitalized header file name, with all disallowed characters substituled wit "_" (like "MYHEADER_H"). You may write whatever you want, but better to stick to the conventions to prevent dissorder. Links: https://www.cprogramming.com/reference/preprocessor/ifndef.html http://www.cplusplus.com/doc/tutorial/preprocessor/ https://wikipedia.org/wiki/Pragma_once
22nd Oct 2017, 8:52 PM
deFault
+ 2
@Rasa Mirzaagasi No, MYCLASS_H != myclass_h. And you cannot use dots in the name. Plus, you need to stick with the name defined, in the same case as earlier. If the 3rd letter is caps in define, it must be so in ifndef as well.
23rd Oct 2017, 1:07 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
The part about 'defining "myclass.h"' isn't clear to me, but if you mean your header file name is "mycalss.h", then yes, MYCLASS_H is a proper conventional token for it's header guard. Though, you may use whatever you like.
22nd Oct 2017, 9:38 PM
deFault
+ 1
Thanks
22nd Oct 2017, 9:39 PM
Rasa Mirzaagasi
Rasa Mirzaagasi - avatar
0
Thank you for your time and now I understood every thing but just an other small question If i define “mycalss.h” is it okey to use ifndeff MYCLASS_H
22nd Oct 2017, 9:32 PM
Rasa Mirzaagasi
Rasa Mirzaagasi - avatar