Open In App
Related Articles

Hygienic Macros : An Introduction

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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++

// C++ program to illustrate a situation known as
// accidental capture of identifiers - an
// undesirable result caused by unhygienic macros
 
#include <iostream>
using namespace std;
 
#define INCI(i) do { int x = 0; ++i; } while(0)
 
int main()
{
    int x = 4, y = 8;
 
    // macro called first time
    INCI(x);
 
    // macro called second time
    INCI(y);
 
    cout << "x = " << x << ", y = " << y << endl;
   
    return 0;
}
 
// This code is contributed by sarajadhav12052009

                    

C

// C program to illustrate a situation known as
// accidental capture of identifiers - an
// undesirable result caused by unhygienic macros
#include <stdio.h>
 
#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, y = %d\n", x, y);
    return 0;
}

                    

The code is actually equivalent to: 
 

C++

// C++ program to illustrate unhygienic macros
// with Macro definition substituted in source code.
 
#include <iostream>
using namespace std;
 
int main()
{
    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);
     
    cout << "x = " << x << ", y = " << y << endl;
    return 0;
}
 
// This code is contributed by sarajadhav12052009

                    

C

// C program to illustrate unhygienic macros
// with Macro definition substituted in source code.
#include <stdio.h>
 
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, y = %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++

// C++ program to illustrate Hygienic macros using
// identifier names such that they do not cause
// the accidental capture of identifiers
 
#include <iostream>
using namespace std;
 
#define INCI(i) do { int m = 0; ++i; } while(0)
 
int main()
{
    int x = 4, y = 8;
     
    // macro called first time
    INCI(x);
     
    // macro called second time
    INCI(y);
     
    cout << "x = " << x << ", y = " << y << endl;
    return 0;
}
 
// This code is contributed by sarajadhav12052009

                    

C

// C program to illustrate 
// Hygienic macros using
// identifier names such that
// they do not cause
// the accidental capture of identifiers
#include <stdio.h>
 
#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




 



Last Updated : 21 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads