In c programming, we can use macros whenever we want to repeatedly use a single value or a piece of code in our programs. By defining macros once in our program, we can use them frequently throughout the program. In addition to this, C also provides predefined macros. This article by Simplilearn will help you understand macros in depth.
What Are Macros in C?
Macro in c is defined by the #define directive. Macro is a name given to a piece of code, so whenever the compiler encounters a macro in a program, it will replace it with the macro value. In the macro definition, the macros are not terminated by the semicolon(;).
Let’s look at the below-given syntax and how the macro is defined in c programs.
Syntax of a Macro:
#define macro_name macro_value;
Example:
#define pi 3.14;
Below we have an example program using macros in c
Example Program:
#include <stdio.h>
#define a 10 //macro definition
int main()
{
printf("the value of a is: %d", a);
return 0;
}
From the above code, “a” is a macro name and 10 is the value. Whenever the compiler encounters a macro name, it will replace it with the macro value.
Output:
Moving ahead, let us know the use of macros in c.
Why Do We Use Macros in C?
The program efficiency is increased by using Macros in c programs. Rather than mentioning a piece of code repeatedly in the programs, we can define the constant value once and use it frequently. Macros in c have functions like macros in which we can pass the arguments, which makes the program run faster.
Example:
#include <stdio.h>
#define date 21 //macro definition
int main()
{
printf("Today's date is: %d/JULY/2022", date);
return 0;
}
Output:
Types of Macros in C
- Object Like Macros
- Function Like Macros
Let us go through them in detail.
Object Like Macros
A macro is replaced by the value in object-like macros. Generally, a defined value can be a constant numerical value.
Let’s look at the examples of how object-like macros are used in c.
Example Program:
#include <stdio.h>
#define pi 3.14 //macro definition
int main()
{
int r = 4;
float circum;
circum = 2*pi*r;
printf("circumference of a circle is: %f", circum);
return 0;
}
Output:
From the above-given code, 3.14 is a value of a macro name pi. In this program, we will calculate a circle's circumference. In the formula 2*pi*r, the pi is replaced by the value 3.14, and the r value is declared in the program.
Consider another example, where we have a macro name pi multiple times in the program.
Example:
#include <stdio.h>
#define pi 3.14 //macro definition
int main()
{
int r = 4, area, circum;
circum = 2*pi*r;
area =pi*r*r;
printf("circumference of a circle is: %d \n", circum);
printf("area a circle is: %d", area);
return 0;
}
From the above-given code, pi is repeated two times in a program. First, it Calculates the circumference of the circle and then calculates the area of the circle. So whenever a compiler encounters the macro, it will replace it with the value 3.14, which is how an object like macros works in c programming.
Output:
Function Like Macros
The way function call happen in C programs. Similarly, the function is defined in functions like macros, and arguments are passed by the #define directive.
We will use the examples to help you understand how to use functions like macros in c.
Example Program:
#include <stdio.h>
#define add(a, b) (a + b) //macro definition
int main()
{
int a = 10, b = 15, result;
result = add(a, b);
printf("Addition of two numbers is: %d",result);
return 0;
}
Once the compiler finds the add (a,b) function, it will replace it with (a+b) and perform the operation.
Output:
Now that you have a brief idea of the types of macros in c., we have a few predefined macros moving ahead.
Predefined Macros in C
C programming language provides several predefined data types and functions. Similarly, c also provides predefined macros that can be used in c programs.
Predefined Macros |
Description |
---|---|
__FILE__ |
It contains the current file name |
__DATE__ |
It displays the current date |
__TIME__ |
It displays the current time |
__LINE__ |
Contains the current line no |
__STDC__ |
When it is compiled, it will return a nonzero integer value |
Look at the below-given example program that illustrates all the predefined macros.
Example Program:
#include <stdio.h>
int main()
{
printf("File name is:%s\n", __FILE__ );
printf("Current Date is:%s\n", __DATE__ );
printf("Current Time is:%s\n", __TIME__ );
printf("The Line no is:%d\n", __LINE__ );
printf("Standard C :%d\n", __STDC__ );
return 0;
}
Output:
With that, you can conclude this tutorial on macros in c.
Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!
Next Steps
"Data Structures in C" can be your next topic. So far, you have learned Macros in C. The next fundamentals will be the data structures and the varieties in data structures used for different purposes.
If you are interested in building a career in software development, then feel free to explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages today? If yes, our Post Graduate Program in Full Stack Web Development should be the right thing for your career. Explore the course and enroll soon.
Please let us know in the comment section below if you have questions regarding the "Macros in C ” tutorial. Our experts will get back to you at the earliest.