C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - True / False Questions (Q.No. 3)
3.
There exists a way to prevent the same file from getting #included twice in the same program.
True
False
Answer: Option
Explanation:

True, We can prevent the same file from getting included again by using a preprocessor directive called #ifndef (short for "if not defined") to determine whether we've already defined a preprocessor symbol called XSTRING_H. If we have already defined this symbol, the compiler will ignore the rest of the file until it sees a #endif (which in this case is at the end of the file).

#ifndef XSTRING_H

#define XSTRING_H defines the same preprocessor symbol,

Finally, the last line of the file, #endif

Discussion:
4 comments Page 1 of 1.

Macik said:   9 years ago
Maybe the answer is not 100% correct. There is no mechanism in C to prevent #including. #include will always include whole file. The mechanism is included in the file itself.

Bunty said:   1 decade ago
#ifndef is a preprocessor directive which means if not defined
For example you made your own file file1.h with a func f1()
Included it in another file file2.h

Now finally in your program
If u include
Include file1.h and file2.h
It will give error
For multiple declaration for function f1()

To avoid this declare the function like this in file1.h

#ifndef FUNF1
#define FUNF1
f1() {/*some code*/}
#endif

So what will happen now
For the first time compiler with go through include file1.h
FUNF1 is not present it will be define and f1()subsequently
Now,when file2.h get included it already has file1 include but this time
As FUNF1 is already defined compiler doesn't go beyond that.

P@1 said:   1 decade ago
Can't understand please explain in detail.

Archana said:   1 decade ago
Can anyone explain in detail.

Post your comments here:

Your comments will be displayed after verification.