I hava a look at many header files they have codes like #ifndef and #endif
what does they explain,please.

Recommended Answers

All 3 Replies

The #ifndef is used for checking if the macro is not already defined, if macro is defined already it will do nothing, if not we can define macro.

below example should clarify your doubt.
to see effect uncomment #define MAX 5

#include <stdio.h>

//#define MAX 5 

#ifndef MAX
#define MAX 10 
#endif

int main()
{
        int cnt = 1 ;
        for(; cnt <= MAX; cnt++)
                printf("%d Hello\n",cnt);
        return 0;
}

To add to clife's explanantion, #ifndef foo is functionally identical to #if !defined(foo).

#ifndef is also frequently used to "guard" header files from being included multiple times, which can be a real problem. Example (myheader.h):

#ifndef MY_HEADER_H
#define MY_HEADER_H
.
.
.
#endif /* MY_HEADER_H */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.