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.
The code is actually equivalent to:
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 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.
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 firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- X-Macros in C
- Macros and its types in C/C++
- Macros vs Functions
- Multiline macros in C
- Branch prediction macros in GCC
- Predefined Macros in C with Examples
- Data Type Ranges and their macros in C++
- Variable length arguments for Macros
- Interesting Facts about Macros and Preprocessors in C
- p5.js | Introduction
- Introduction to SQLite
- AA Trees | Set 1 (Introduction)
- Introduction To APIs
- Introduction to WebRTC
- Introduction of a Router
- Introduction of Hyperledger
- Introduction to Ontologies
- An introduction to Flowcharts
- Introduction of MariaDB
- Namespace in C++ | Set 1 (Introduction)