We are all familiar with the working of macros in languages like C. There are certain situations in which macro expansions can lead to undesirable results because of accidental capture of identifiers.
For example:
// C program to illustrate a situation known as // accidental capture of identifiers - an // undesirable result caused by unhygienic macros #define INCI(i) do { int x = 0; ++i; } while(0) int main(void) { int x = 4, y = 8; // macro called first time INCI(x); // macro called second time INCI(y); printf("x = %d, b = %d\n", x, y); return 0; } |
The code is actually equivalent to:
// C program to illustrate unhygenic macros // with Macro definition substituted in source code. int main(void) { int x = 4, y = 8; //macro called first time do { int x = 0; ++x; } while(0); //macro called second time do { int x = 0; ++y; } while(0); printf("x = %d, b = %d\n", x, y); return 0; } |
Output:
x = 4, y = 9
The variable a declared in the scope of the main function is overshadowed by the variable a in the macro definition so a = 4 never gets updated (known as accidental capture).
Hygienic macros
Hygienic macros are macros whose expansion is guaranteed not to cause the accidental capture of identifiers. A hygienic macro doesn’t use variable names that can risk interfering with the code under expansion.
The situation in the above code can be avoided simply by changing the name of the variable in the macro definition, which will produce a different output.
// C program to illustrate // Hygienic macros using // identifier names such that // they do not cause // the accidental capture of identifiers #define INCI(i) do { int m = 0; ++i; } while(0) int main(void) { int x = 4, y = 8; // macro called first time INCI(x); // macro called second time INCI(y); printf("x = %d, y = %d\n", x, y); return 0; } |
Output:
x = 5, y = 9
This article is contributed by Palash Nigam. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Multiline macros in C
- Variable length arguments for Macros
- Branch prediction macros in GCC
- Macros vs Functions
- Data Type Ranges and their macros in C++
- X-Macros in C
- Predefined Macros in C with Examples
- Macros and its types in C/C++
- Interesting Facts about Macros and Preprocessors in C
- Introduction of Smart Pointers in C++ and It's Types
- Namespace in C++ | Set 1 (Introduction)
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Understanding "volatile" qualifier in C | Set 1 (Introduction)
- C Language Introduction
- PHP | MySQL Database Introduction
- An introduction to Flowcharts
- Introduction to Hill Climbing | Artificial Intelligence
- AA Trees | Set 1 (Introduction)
- Fuzzy Logic | Introduction

