Macros in C – Types and Examples

C programming offers various types of functions and methods to the programmers. It helps in turning the large and complex codes into simple and easy. In C, the program code is first compiled and then it is sent to the compiler.

The compiler turns it into machine language and then the compilation finishes and the C program gets executed. A macro in C is a set of program statements that is replaced by the value of the macro throughout the entire program.

Let us learn about macros in C.

What is a Macro in C?

You can say that macro is a piece of code that is replaced by the value of the macro throughout the program. You can define a macro with #define directive. At the end of defining a macro, you don’t have to put a semicolon(;) to terminate it.

Types of Macros in C

There are 2 types of macros present in C such as:-

  • Object-like Macros.
  • Function-like Macros.

1. Object-like Macros in C

It is a simple type of macro. In this object-like macro, the macro will be replaced by it’s value. Object-like macros mainly used to represent numeric constants.

Example:-

#define PI 3.14 

Example:- Object-like Macros

#include <stdio.h>
#define SIDE 4
int main() {
  int area;
  area = SIDE*SIDE;
  printf("TechVidvan Tutorial: Object Like Macros!\n");
  printf("Area is: %d",area);
  return 0;
}

Output:-

TechVidvan Tutorial: Object Like Macros!
Area is: 16

2. Function-like Macros in C

In C, function-like macros are much similar to a function call. In this type of macro, you can define a function with arguments passed into it.

Example:-

#define AREA(a) (a*a)

Example:- Function-like Macros

#include <stdio.h>
#define AREA(s) (s * s) // macro with argument
int main()
{
int s1 = 10, area_of_square;
area_of_square = AREA(s1);
printf("TechVidvan Tutorial: Macros with arguments!\n");
printf("Area of square is: %d", area_of_square);
return 0;
}

Output:-

TechVidvan Tutorial: Macros with arguments!
Area of square is: 100

In the above example, the compiler finds the name of the macro (AREA(a)) and replaces it with the statement (a*a).

Predefined Macros in C

There are some predefined macros present in C. And it cannot be modified. Below are some predefined macros.

MACRO  What it does
__DATE__ Current date as MMM DD YYYY format.
__TIME__ Current time as HH:MM:SS format.
__FILE__ Contains current filename.
__LINE__  Contains current line number.
__STDC__ Defined as 1 when the compiler compiles.

Example:- Predefined Macros

#include <stdio.h>
int main() {
printf("TechVidvan Tutorial: Predefined Macros!\n\n");
char filename[] = __FILE__;
char date[] = __DATE__;
char time[] = __TIME__;
int line = __LINE__;
int ansi = __STDC__;
printf("File name is: %s\n", filename);
printf("Date is: %s\n", date);
printf("Now time is: %s\n", time);
printf("Current line number: %d\n", line);
printf("Compilation Success: %d\n", ansi);
}

The above code is saved in a file named HelloWorld.c.
Output:-

TechVidvan Tutorial: Predefined Macros!

File name is: main.c
Date is: Jun 24 2021
Now time is: 09:47:33
Current line number: 15
Compilation Success: 1

Before using Macros, you have to be careful on following points:-

1. In C, when we define a macro then it is replaced by the value of that macro. And it will be set for the entire program. With #define directive, you can define a macro.

For Example:-

#include<stdio.h>
#define A 24
int main()
{
printf("Value of A is %d", A);
return 0;
}

Output:-

Value of A is 24

2. You can also write multiple lines during defining the macro. You can do that with “\”. It is known as a Continuation operator in Macro.

For Example:-

#define  message_of_the_day(i, j)  \
   printf(#i " and " #j ": TechVidvan Tutorial: Macro Continuation!")

You can also use macro as a function. And you can pass arguments to it.

#include <stdio.h>
#define ADD(x,y) x+y
int main()
{
printf("%d", ADD(3,2));
return 0;
}

Output:-

5

3. In C, you can also convert a macro parameter into a string. You have to use Stringize operator(#) to do so.

#include <stdio.h>
#define statement(i)  \
  printf(#i ": Macro Stringize!\n")
 
int main(void) {
   statement(TechVidvan Tutorial);
   return 0;
}

Output:-

TechVidvan Tutorial: Macro Stringize!

While defining, you can also use conditional statements such as if-else directives. This is known as Conditional Compilation.

Why to use Macros in C?

  • It becomes handy when you use it for anything magic number or string related.
  • You can use macros to create automatic loop unrolling.
  • With macros, you can do some things which you cannot do with functions like Token Pasting.
  • You can use a macro for writing debugging messages also.

Summary

In this tutorial, we learnt about macros in C. We also discussed the types of macros in C. You can define a macro with #define directive. In C, A macro is any constant value or variable with its value.

And the macro name will get replaced by its value in the entire program. Macros help in writing less code and also in saving time.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.