#ifdef and
#ifndef check to see if the symbol has been defined.
#ifdef then includes everything up to an
#else or an
#endif if the symbol
has been defined, while
#ifndef includes everything if the symbol
has not been defined.
So, in your example, the first time you include
math.h, assuming that is the name of that file, the pre-processor will find the
#ifndef _MATH_H_ line, find that it has not been defined and continue including code.
The next line then defines
_MATH_H_ as the default value, which should be 1.
If you then try to include
math.h again, the pre-processor will get to the
#ifndef line, discover that
_MATH_H_ has been defined and skip to the
#endif.
so why would i ever need to use that in a program? What will happen if i don't.
|
Well, People do this to stop a header file from being included multiple times.
example.h
1 2 3 4 5 6
|
#ifndef _EXAMPLE_H
#define _ EXAMPLE_H
int exampi = 0;
#endif
|
example.c
1 2 3 4
|
#include "example.h"
#include "example.h"
...
|
This example then, tries to include
example.h twice, however, the second time it tries, it discovers that
_EXAMPLE_H has been defined, and skips to the
#endif, thus stopping the file from being included twice. If you didn't have this code the compiler would spit errors about defining the same variable twice.