Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction💁
2.
Example📝
3.
Types Of Macros
3.1.
Object-Like Macros
3.2.
Function-Like Macro
3.3.
Multi-Line Macros
4.
Pre-Define Marcos📝
4.1.
Example
5.
Frequently Asked Questions
5.1.
Where are macros stored in C?
5.2.
What is the use of macros in C?
5.3.
What are the benefits of using macros over functions in C?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C Macros

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction💁

A macro is a section of code in a program that is replaced by the macro's value. The #define directive defines a macro. When the compiler encounters a macro name in the code, it replaces it with the defined value or expression of the macro. 

In simple terms, macros are used to define any constant values or any expression such that it can be used anywhere in the code by just calling the macro name.

Introduction

Recommended Topics, Sum of Digits in C and C Static Function.

Example📝

To understand them better, let us look at a basic example of C Marcos.

#include <stdio.h>

#define PI 3.14
#define codingNinja "Coding Ninjas"
int main()
{
    printf("\nThe value of PI is = %f", PI);
    printf("\nHey Ninja welcome to %s \n\n", codingNinja);
    return 0;
}
Output

You can observe that we "define" the value of PI and codginNinja using the #define directive outside the main() function, yet we can use it anywhere in the program. 

Types Of Macros

C programming language has three types of macros. We will discuss all three of them in this section.

  • Object-Like Macros
  • Function-Like Macros
  • Multi-Line Macros

Object-Like Macros

The object-like macro is similar to what we saw in the example in the introduction section. The PI defined in the previous code was an example of object-like macros. The object-like macros are mainly used to define some constant value, be it numeric or string. It is simply like an identifier that will be replaced by its value whenever the compiler finds the macro identifier. Let us look at an example,

// C program to demonstrate object-like macros.
#include <stdio.h>

// The identifier "PRESENT_YEAR" here is a macro.
// Whenever the compiler will find the identifier "PRESENT_YEAR" anywhere in the code
// it will replace it with the value of 2022
#define PRESENT_YEAR 2022
int main()
{
    // PRESENT_YEAR gets replaced with its numeric value -> 2022 
    printf("\n\nThe current year is = %d\n\n", PRESENT_YEAR);
    return 0;
}
Output 1

In the above code, the identifier "PRESENT_YEAR" is a macro. It is defined using the #define preprocessor directive. Whenever the compiler finds the identifier "PRESENT_YEAR" anywhere in the code, it will replace it with the value of 2022.

Function-Like Macro

Function-like macros are similar to normal functions in C. It can take arguments, just like any function. In order to define a macro that uses arguments, we insert parameters in parentheses while defining the macro. The parameters must be a valid C identifier, separated by commas. We can also add white spaces. Let us look at an example of function-like macros,

// C program to demonstrate function-like macros.
#include <stdio.h>
#define PI 3.14
// function-like macro
// the compiler will replace the macro CIRCUMFERENCE everywhere with its value.
// We can use this macro just as a normal function.
// pass the value of 'R' as the parameter, and it will return the value of circumference. 
#define CIRCUMFERENCE(R) (2*PI*R)
int main()
{
    // using the macro CIRCUMFERENCE.
    // To get the circumference of a circle with radius 5 units
    // we call CIRCUMFERENCE(5) as shown
    printf("\n\nThe circumference of the circle of radius 5 units is = %f\n\n\n", CIRCUMFERENCE(5));
    return 0;
}
Output 2

In the above code, the function CIRCUMFERENCE(R) is the macro. We do not define the data type of R inside the parameter. Unlike object-like macros, it is mandatory to provide parenthesis to the function-like macros. 

Multi-Line Macros

The multi-line macro is just an extension of an object-like macro where we can write multiple lines of code. In order to make a multi-line macro, we use the backslash-newline ( "\" ). Let us look at an example to better understand multi-line macros. you can also implement code with the help of a C compiler.

#include <stdio.h>
// Multi-line Macro definition
#define MULT_LINE_MACRO 100, \
200, \
300
int main()
{
    // Array arr[] with elements
    // defined in macros
    int a[] = { MULT_LINE_MACRO };
    // Print elements
    printf("\n\nElements of Array are:\n");
    for (int i = 0; i < 3; i++) {
        printf("%d ", a[i]);
    }
    printf("\n\n");
    return 0;
}
Output 3

Pre-Define Marcos📝

There are certain predefined macros in C. These predefined macros cannot be modified by the user in the program. Below is the list of the predefined macros in C, along with their application. 

Note: There are two underscores(_), each before and after the name of the macros.

  • __DATE__: This C macro is used to display the current date in a string format.
  • __TIME__: This C macro is used to display the current time in a string format. 
  • __FILE__: This C macro is used to display the file name of the code.
  • __LINE__: This C macro is used to display the line of the code.
  • __STDC__: This C macro displays 1 if the compiler uses ANSI Standard.
Pre-Define Marcos

Example

// C program to demonstrate predefined macros in C
#include <stdio.h>
int main()
{
    // __DATE__ 
    printf("\n\nToday is : %s \n",__DATE__);
    
    // __TIME__
    printf("\nThe current time is : %s\n",__TIME__);
    
    // __FILE__
    printf("\nThe current file is : %s\n",__FILE__);
    
    // __LINE__
    printf("\nThe current line number is : %d\n",__LINE__);
    
    // __STDC__
    printf("\nThe value returned by __STDC__ is : %d\n\n\n", __STDC__);  
    return 0;
}
Output 4

 

Now let's discuss some frequently asked questions associated with the custom interceptors.

FAQs

You can also read about dynamic array in c, and  Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

Where are macros stored in C?

The macros in C are not stored in the memory. Instead, the macros are called every time they are encountered in the code.

What is the use of macros in C?

The macros in C are used to reduce code redundancy and make the code more efficient and easy to read.

What are the benefits of using macros over functions in C?

The macros in the C programming language are faster and more efficient than functions in the C programming language.

Conclusion

In this article, we have extensively discussed macros in C programming language along with the code examples to get a better understanding of macros in C. Learn more about functions in C here. You can explore all the blogs related to C here.

Do not forget to refer to our guided paths for complete interview preparation guidance. Check out this amazing Data Structures and Algorithms course and enhance your DSA skills. You can also try the Competitive Programming course and upskill your CP skills.

Check out the free mock test series to test your knowledge. If you are preparing for tech interviews, you should definitely check out the problemsinterview experiences, and interview bundle for placement preparations.

Previous article
What is Include Stdio in C/C++?
Next article
#include in C
Live masterclass