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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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:

Macros_in_C_1

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:

Macros_in_C_2

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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:

Macros_in_C_3

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:

Macros_in_C_4

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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:

Macros_in_C_5.

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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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:

Macros_in_C_6.

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.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 24 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449