Macros in C [Explained With Examples]
Hello Friends, In today’s article we are going to talk about C Macros. So without wasting any time let’s understand what is macros in c
Macros in C
Macros are a type of identifiers that are usually written in capital letters to distinguish them from normal variables.
The #define preprocessor directive is used to define a macro. Its basic syntax is something like this –
Syntax -:
#define macro_name macro_expansion
For example -:
#deflne TRUE 1
#define FALSE 0
#define PI 3.14159265
#define MAX 100
#define RETIRE AGE 58
- #define is a preprocessor directive used to define macros.
- macro_expansion can be any text. (eg – TRUE, FALSE, PI, MAX, MIN etc.)
- There must be a space between macro_name and macro_expansion.
- In C language, when a macro is triggered, the preprocessor replaces macro_name with macro_expansion.
- The macros definition does not need to be terminated by a semi-colon (;).
Let’s understand macros better through a program.
Example Program
#include <stdio.h>
// macro definition
#define age 18
int main()
{
int userA;
printf("Enter Your Age : ");
scanf("%d",&userA);
if(userA>age)
{
printf("You are eligible for voting\n");
}
else
{
printf("Not eligible for voting \n");
}
return 0;
}
In this program, I have defined a macro named age whose value is 18. Wherever this macro (age) is used in the program, the preprocessor will replace that macro name with its value (18) in its place.
In this program, I have used age macro with if condition and find out whether user is eligible for voting or not?
As the program passes the preprocessor, the C preprocessor replaces the age macro with its value 18.
The output of this program will be something like this.
Output -:
Enter Your Age : 24
You are eligible for voting
Types of Macros
There are three types of macros in C language:
- Object like macros
- Function like macros
- Chain like macros
1) Object like macros
Object-like macros are simple identifiers that are replaced by value.
It is widely used to represent numeric constants values in symbolic names.
For Example -:
#define PI 3.14
Here PI is a macro which will be replaced by the value 3.14.
Example Program
// C program to illustrate macros
#include <stdio.h>
// Macro definition
#define PI 3.14
int main()
{
int r, aoc;
// Print the message
printf("Enter the radius :");
scanf("%d",&r);
aoc = PI*(r*r);
printf("Area of circle is %d",aoc);
return 0;
}
Output -:
Enter the radius : 5
Area of circle is 78.54
2) Chain like macros
When a macro is defined inside a macro, it is called chain macros. In chain macros, first the parent macro is expanded, then the child macro is expanded.
Example -:
// C program to illustrate chain macros
#include <stdio.h>
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 330
int main()
{
// Print the message
printf("CsTutorialpoint have %dK followers on Instagram", INSTAGRAM);
return 0;
}
Output -:
CsTutorialpoint have 330k followers on Instagram
3) Function like macros
Function-like Macros look like function calls. The parentheses() is required immediately after the macro name. If we put a space between macro name and parentheses in macro definition then macro will not work.
Example -:
// C program to illustrate Function like macros
#include <stdio.h>
// Function-like Macro definition
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
int main()
{
// Given two number a and b
int a = 10;
int b = 70;
printf("Minimum value between"
" %d and %d is %d\n", a, b, min(a, b));
return 0;
}
Output -:
Minimum value between 10 and 70 is 10
There are many macros defined in ANSI C which are used for different purposes in C language.
Let’s know about those predefined macros
Predefined macros In C
No. | Macro | Description |
1 | _DATE_ | To represent the current date in the format “MMM DD YYYY”. |
2 | _TIME_ | To represent the current time in the format “HH:MM:SS”. |
3 | _FILE_ | Represents the current file name. |
4 | _LINE_ | Represents the current line number. |
5 | _STDC_ | It is defined as 1 when the compiler compiles the ANSI standard. |
Let us understand these predefined macros with examples.
Example
// C program to illustrate predefined macros
#include <stdio.h>
int main(){
printf("File :%s\n", __FILE__ );
printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("STDC :%d\n", __STDC__ );
return 0;
}
Output -:
File :program.c
Date :Sep 01 2021
Time :12:28:46
Line :6
STDC :1
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is Macros In C and why and how they are used in C language.
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Macros In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.