What is the meaning of #ifndef ??? is it like #define or if condition ?
Printable View
What is the meaning of #ifndef ??? is it like #define or if condition ?
if not defined.
when you define something with
#define something
the compiler won't process what is between
#ifndef something
and the next
#endif something
where something obviously could be replaced with anything. if you remove the #define line, the lines will be processed.
Kedaman
Thank you very much ! But here I have some code like this
#ifndef XYZ
#define XYZ
one structure declaration
#endif
Here what will happen ?
This is to make sure that the structure will be declared only once:
First time the compiler sees this:
#ifndef _STR_DEF
_STR_DEF is not defined, so the compiler (actually the precompiler) will execute the code:
#define _STR_DEF
struct str
.
.
.
#endif
Due to nested including, the same header is included a second time, but this time, _STR_DEF is already defined, so the compiler will skip the struct definition, thus preventing an error.
This method is commonly used to prevent headers from being included twice:
#ifndef _MY_HEADER_INCLUDED_
#define _MY_HEADER_INCLUDED_
.
.
.
#endif // _MY_HEADER_INCLUDED_
in VC++, you could also use:
#pragma once
but that is not portable.
All the buzzt
CornedBee