Signup/Sign In
LAST UPDATED: APRIL 3, 2023

What are Macros in C Language and its Types

Technology #c-language#macros

    In this article, We will discuss what macros are in c and what are its different types. So, Let's start with trying to understand what macros are in C. Sometimes when we are programming a large project we generally write our code and then compile it using a compiler and generally 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 and save our time, 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

    Conclusion

    In this article, we have seen what macros are 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 that are Object-like Macro, Function-like Macro, Multi-line Macro and Chain Macro. 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.

    Frequently Asked Questions

    1: What are macros in C language?

    Macros in C language are preprocessor directives that are used to define constants, functions, and code snippets. They are identified by the # symbol at the beginning of a line.

    2: What are the benefits of using macros in C language?

    The benefits of using macros in C language include improved code readability and maintainability by allowing developers to define and reuse code snippets, constants, and functions. They also make it easier to modify and update code by changing the definition of a macro.

    3: What are the two types of macros in C language?

    The two types of macros in C language are object-like macros and function-like macros. Object-like macros define constants or code snippets, while function-like macros define code snippets with arguments.

    4: How do I define and use macros in C language?

    To define a macro in C language, use the #define directive followed by the name and definition of the macro. To use a macro in your code, type its name and the preprocessor will replace the macro name with its definition during compilation.

    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!

    I am the founder of Studytonight & Fullstack developer (MERN). I like writing content about ReactJS, MERN, JavaScript, Docker, Linux, PHP, Go lang, Cloud, Web development, and general Tech related content. I have 10 years of diverse experience in software development.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS