Signup/Sign In

What are Macros in C Language and its Types

Posted in Programming   OCTOBER 15, 2022

    Let's start with trying to understand what are macros in C. We write our code and then compile it using a compiler. Sometimes there are some numbers or pieces of code that are repeated many times in the code. In order to avoid writing that code many times, we use macros in C programming.

    What are Macros in C?

    We can say that macro is a code or a set of statements that gets replaced by its value during compilation time.

    We use the #define preprocessor directive to include C macro functions and objects in our code. It is good practice to declare macros in upper-case alphabets in order to distinguish them from other identifiers. They do not utilize any memory and are different from other variables in the code. We do not need to use a semicolon to terminate C macro functions.

    Types of Macros

    Macros are of the following types -

    1. Object-like Macro

    It is a simple macro that gets replaced with some numeric value represented as a constant.

    In the code below, 5 is a constant, and whenever we use MAX, it will denote 5.

    For example,

    #include <stdio.h>
    #define MAX 5
    
    int main() {
        int a[100];
        for(int i = 0; i < MAX; i++)
            scanf("%d",&a[i]);
        int sum = 0;
        for(int i = 0; i < MAX; i++)
            sum += a[i];
        printf("The sum is: %d", sum);
        return 0;
    }
    


    3 4 5 6 2
    The sum is: 20

    2. Function-like Macro

    This macro replaces the function code with its name so that we don't have to write the complete function again and again.

    The function CUBE() in the below code has been defined only one time but we can use it as many times as we want because we have declared it as a macro.

    #include <stdio.h>
    #define  CUBE(r)  ((r)*(r)*(r))
     
    int main() {
        int a = 5, b = 6;    
        printf("Cube of %d is %d\n", a, CUBE(a));
        printf("Cube of %d is %d", b, CUBE(b));
        return 0;
    }


    Cube of 5 is 125
    Cube of 6 is 216

    3. Multi-line Macro

    As the name suggests, multi-line macros are declared in multiple lines using a backslash.

    In the example below, we have declared the array elements in multiple lines using backslashes which makes it a multi-line macro.

    #include <stdio.h>
    #define ARRAY_ELEMENTS  1, \
                            3, \
                            7, \
                            6, \
                            6, \
                            7
    
    int main() {
        int a[] = { ARRAY_ELEMENTS };
        int sum = 0;
        for(int i = 0; i < 5; i++)
            sum += a[i];
        printf("The sum is : %d", sum);
        return 0;
    }
    


    The sum is: 23

    4. Chain Macro

    When we nest a macro inside a macro, it is called a chain macro. Firstly, the compiler expands the outer macro and then the inner one.

    In the code below, firstly, we expand TOTAL, then NUM, and then MAX.

    #include <stdio.h>
    #define TOTAL NUM
    #define NUM MAX
    #define MAX 5
    
    int main() {
        int a[100];
        for(int i = 0; i < MAX; i++)
            scanf("%d",&a[i]);
        int sum = 0;
        for(int i = 0; i < MAX; i++)
            sum += a[i];
        printf("The sum is: %d", sum);
        return 0;
    }
    


    1 2 3 4 5
    The sum is: 15

    In this article, we have seen what are macros in C programming and how they are useful. A macro is replaced by a piece of code in our program during compilation. They are declared using #define directive and are of many types. They help save a lot of time and make coding an efficient process. There are built-in macros too. You can learn more about them here.

    Not yet confident about your knowledge in C? Our free tests will help you achieve confidence in a few easy steps. You can always follow our C tutorial here when in doubt. As they say, never stop learning!

    Published by: aashaykumar
    Tags:macrosc-language
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS