Macros in C Language – Types and Examples

Macros in C Language – 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.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

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.


define PI 3.14

Example:- Object-like Macros


define SIDE 4

int main() {
int area;
area = SIDE*SIDE;
printf(“TechVidvan Tutorial: Object Like Macros!\n”);
printf(“Area is: %d”,area);
return 0;

TechVidvan Tutorial: Object Like Macros!
Area is: 16

  1. 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.


define AREA(a) (a*a)

Example:- Function-like Macros


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;

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


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.

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:-


define A 24

int main()
printf(“Value of A is %d”, A);
return 0;

Value of A is 24

  1. 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.


define ADD(x,y) x+y

int main()
printf(“%d”, ADD(3,2));
return 0;


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


define statement(i) \

printf(#i “: Macro Stringize!\n”)

int main(void) {
statement(TechVidvan Tutorial);
return 0;

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.
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.

Categories: C Tutorials
Tags: Macros in C, Types of C Macros

Leave a comment