C Programming

Macro in C language

A macro in C language is a piece of code which has been assigned a name. When the name is used anywhere in the program, the macro value is replaced before the program’s compilation. In this article, we will see in detail how to write a macro in C language.

Consider the following code:

START

    INTEGER n=5;
   
    PRINT("Value of n is %d",n);

END

The above code is not a valid C code.

But the following code is valid:

//Example1.c
#define START int main(){
#define END }
#define INTEGER int
#define PRINT(A,B) printf(A,B)


START

    INTEGER n=5;
   
    PRINT("Value of n is %d",n);

END

Before compilation, the macro START, INTEGER, PRINT and END has been replaced by their value, and the code becomes a valid C code. We can check using the following command:

gcc –E Example1.c

This command will show after expanding all the macros.

Now we will see different types of macros:

1. Object-like macros:

Syntax:

#define macro_name  macro_value
  • Macro is always starting with #define
  • macro_name is a user-defined name of the macro
  • macro_value is the value of the macro. It may be anything, but one line and the macro body ends with that line’s ends. It does not require semicolon (;) at the end. Space is also considered.

If the macro takes more than one line we can do it as follows:

#define macro_name  macro_value1 \
                    macro_value2 \
                    macro_value3


#define MAX 200

This macro looks like a data object that’s why this type of macro called as an object-like macro.

//Example2.c  
//#include <stdio.h>
#define MAX 200  
     
int main()  
{  
    printf("MAX Value is: %d",MAX);  
    return 0;
}

In Exapmle2.c, MAX is a macro. From the output, we observe that MAX is replaced by its value 200.

2. Function-like macros:

Syntax:

#define macro_name()  macro_value

macro_name is a user-defined name of the macro. Pair of parenthesis has to put after the macro_name. No space is allowed between macro_name and parenthesis. We also can pass arguments in this type of macros.

#define add(x,y) x+y

This macro looks like a function call that’s why this type of macro called a function-like macro.

//Example3.c  
     
#define add(x,y) x+y  
     
int main()  
{  
     
    int a;  
    float b;  
     
    a = add(4,5);  
    b = add(2.5,3.6)  
         
    return 0;
}

In Example3.c, we have seen that unlike C’s function, the macro replace only the code with arguments without calculating it. So, we can pass different data type using the same macro.

If we put a space between the macro name and parenthesis, it works the same as an object-like macro. Below C Example illustrates this.

//Example4.c  
 
#define add (x,y) x+y  
 
int main()  
{  
 
    int a;  
    float b;  
     
    a = add(4,5);  
    b = add(2.5,3.6)
}

In Example4.c, we have seen that the macro add is replaced by (x,y) x+y . Same as an object-like macro.

3. Macro for Token Pasting:
In C language, ## operator is used for token pasting. Using this operator, we can combine two valid tokens into one valid token.
Example:

//Example5.c  
#define MARGE(x,y) x##y  
 
int main()  
{  
 
    int num = MARGE(52,34);  
    return 0;
}

If we try to token pasting that does not generate a valid token, the C compiler gives an error or warning.

//Example6.c  
#define MARGE(x,y) x##y  
 
int main()  
{  
 
    int num = MARGE(52,+);  
    return 0;
}

In Example6.c, we have an error message because, after a combination of two tokens, we get an invalid token ’52+’.

4. Macro for Stringizing:
In C language, # operator is used to converting a macro parameter into a string constant. When a # operator precedes with a macro parameter, the parameter converts to a string literal. Stringizing can be used for object-like and function-like macros.
Example:

//Example7.c  
#define STRINGIZING(x) #x  
 
int main()  
{  
 
    printf(STRINGIZING(Hello World) );  
    return 0;
}

In Example7.c we have got a string “Hello World” using STRINGIZING macro.

Conclusion:

This article has learned about all types of macro-like Object-like macros, Function-like macros, Macro for Token Pasting, Macro for Stringizing and Macro for Stringizing in C language. Now we can use a macro in our C program without any doubt.

About the author

Bamdeb Ghosh

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com