I have three files
File "grandparent.h"
#ifndef GRANDPARENT_H #define GRANDPARENT_H struct foo { int member; }; #endif /* GRANDPARENT_H */ File "parent.h"
#include "grandparent.h" File "child.c"
#include "grandparent.h" #include "parent.h" Wiki says
Here, the first inclusion of "grandparent.h" causes the macro GRANDPARENT_H to be defined. Then, when "child.c" includes "grandparent.h" the second time, the #ifndef test returns false, and the preprocessor skips down to the #endif, thus avoiding the second definition of struct foo. The program compiles correctly.
q1. "the first inclusion of "grandparent.h" causes the macro GRANDPARENT_H to be defined", So what i understand i its basically defining a macro named GRANDPARENT_H but what i dont understand is that how will the content of that macro (i.e GRANDPARENT_H) would be included in the child.c.
We are just defining the macro GRANDPARENT_H i.e
#define GRANDPARENT_H struct foo { int member; }; but how will its content i.e
struct foo { int member; }; be included in the child.c
gcc -Eto get the code after preprocessor has done its thing, check the documentation for the compiler you're using if not gcc. Clang uses the same-Eflag it seems (clang.llvm.org/docs/CommandGuide/clang.html)#define FOO 123. Then the preprocesseur symbolFOOis defined and each time you writeFOO,FOOwill be replaced by123. But you can also write#define FOO. Then the preprocesseur symbolFOOis also defined and each time you writeFOO,FOOwill be replaced by, you've guessed it, an empty string.