Answer: In simple terms #include containing other #include directives included in the program. For ex.,
/* sll.h */ #include <stdio.h> #include <stdlib.h> #define BUFFSIZE 512 /* function declarations to be used in a program */ void sum2ints(const int, const int); void sum2floats(const float, const float); /* other declarations */
Notice that #include “sll.h” contained, together with other declarations, two more #includes: stdio.h and stdlib.h. Actually, C programs generally contain several library functions which are prototyped in stdio.h and stdlib.h.
Standard requires that nested #include files be supported to a depth of at least eight, but it doesn’t impose a maximum limit on nesting depth. A disadvantage of nested #include files is that they make it difficult to determine the true dependencies of source files on each other. A program on Linux, “make utility” must know these dependencies in order to determine which files need to be compiled after some files are modified. Another disadvantage of #include files is the possibility that one header file being included multiple times. Let’s illustrate this error, for ex.,
#include "a.h" #include "a.h"
Although, no one would ever make such a mistake. But what happens if one does like as
#include "a.h" #include "b.h"
and each #include a.h and b.h contains a nested #include of x.h. Once again #include x.h is included twice and only this time it’s not as obvious.
Multiple inclusion occurs most often in larger programs with multitude of header files, thus it’s not easy to find. How can one prevent it from occurring in programs? The solution is easy with conditional compilation. Let’s understand this right away,
If you define all your’s header declarations as,
#include _HEADERNAME_H #define _HEADERNAME_H 1 /* * Write here all your stuff you want in this header file */ #endif
then the risks of multiple inclusions are eliminated. Therefore, the first time when the header file is included it’s processed normally and the symbol _HEADERNAME_H is defined to be 1. When it’s included again, this time preprocessor ignores it because of conditional compilation. Symbol _HEADERNAME_H is named after the filename to avoid any conflicts with other similar symbols in the other header files.
Keep in mind, though, that the preprocessor reads the entire contents of header file, even if the whole file is ignored. Because of this, processing slows down compilation and therefore multiple inclusions whether by nested #include’s or not should be avoided when possible.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.