An Absolute Guide to Learn Everything on Macros in C | Simplilearn (2024)

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 Development-MEANExplore Program

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (1)

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:

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (2)

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:

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (3)

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (4)

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:

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (5)

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:

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (6)

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (7)

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:

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (8)

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 Development-MEANExplore Program

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (9)

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:

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (10)

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.

An Absolute Guide to Learn Everything on Macros in C | Simplilearn (2024)
Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6096

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.