EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Macros in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Macros in C

Macros in C

Introduction to Macros in C

In C programming language, when the C program is compiled then the program is sent to the compiler which converts the C program into machine language and then the compilation is done and executes the C program. The C preprocessor is also known as a macro preprocessor. The macro in C can be defined as a set of program statements is given a name and this name is used whenever these set of code has to be used. These macros always start with symbol “#” and the statements staring with this symbol are invoked by the compiler. There are two types of macros: object- like macro when data objects are used and function-like macro when function calls are used.

Examples of Macros in C

A macro in C programming language is a block or set of statements of program code that can be replaced by macro #define directive. As discussed above there are two types of macros. They are:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Object-Like Macro

This macro can be a pointer variable or identifier or variable defined using #define and it is replaced by its value. This is mainly used when numeric constants are required.

Example:

#define MAX 100

In this example, we can use “MAX” as a macro name with value 100 which will be replaced with “MAX”.

2. Function-Like Macro

This macro is similar to a function call in C programming language. In this, a function name is defined with arguments passed to the function.

Example:

#define ADD (p, q) (p) + (q)

In the above example, we can function named “ADD” is defined to perform the addition of two numbers “p” and “q”.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Before using macro in C programming we should note the following points:

  • In C language, when we use “define” for defining a macro with “#” prefixed is used for defining constants for the entire program, so when we define a constant with some macro name then in the entire program the defined constant is matched and is replaced with the value that is assigned at the starting of declaring the macro with “#define” statement. Example:

#include<stdio.h>
#define PI 3.14
intmain()
{
printf("The value of Pi is %f", PI);
return 0;
}

Output:

Macros in C-1.1

Similarly “#include” is used to include the header files in the C program. For example, we saw in the above code “#include<stdio.h>” in this “stdio.h” is a header file we use this header file because to read and print the values we use “printf” and “scanf” are within this “stdio.h” header file.

  • You should note that when the argument passed to the function-like macro where it supports any datatype. Let us consider an example below:

Example:

#include <stdio.h>
#define INCREMENT(n) ++n
intmain()
{
char *p = "Educba";
int a = 20;
printf(" This use char datatype %s ", INCREMENT(p));
printf("\n This uses int datatype %d", INCREMENT(a));
return 0;
}

Output:

Macros in C-1.2

  • The macro can also be written in multiple lines starting the statements with “\” and no need to end with “\”. Let us see an example of this:

Example:

#include <stdio.h>
#define MACRO(n, limit) while (n < limit) \
{ \
printf("Educba "); \
n++; \
}
intmain()
{
int n = 0;
MACRO(n, 5);
return 0;
}

Output:

Macros in C-1.3

  • In C, the macro can use conditional statements like if-else directives also while defining that can be used for conditional compilation. Let us consider an example:

Example:

intmain()
{
#if VERBOSE >= 2
printf("Message");
#endif
}

No Output:

  • If the macro is used for a function-like macro which has arguments passed to it are not evaluated before macro expansion. Let us see the example:

Example:

#include <stdio.h>
#define DIV(a, b) a/b
intmain()
{
printf("%d", DIV(5+3, 3+2));
return 0;
}

Output:

Macros in C-1.4

Here in the above macro is expanded as 2 +3 /3 + 5 = 2 + 1 + 5 =8 which is confusing so you can resolve by below modified macro.

#include <stdio.h>
//here, instead of writing a*a we write (a)*(b)
#define DIV(a, b) (a) / (b)
intmain()
{
printf("%d", DIV(5+3, 3+2));
return 0;
}

Output:

Output-1.5

The macro is expended as (2 + 3) * (3 + 5), as 5*8 = 1

  • The macro also supports to pass the tokens to the macro to concatenate tokens using the special operator “##” which is known as the token pasting operator. Let us see an example:

Example:

#include <stdio.h>
#define concate(p, q) p##q
intmain()
{
printf("%d ", concate(56, 78));
}

Output:

Output-1.6

The above code defines the function-like macro where the token is passed to concatenate two values of “p” and “q”.

  • The macro also can be used to pass the token that can be converted to a string by using the “#” special symbol before the token. Let us see an example:

Example:

#include <stdio.h>
#define get_string(n) #n
intmain()
{
printf("%s", get_string(Educba));
}

Output:

Output-1.7

In the above code, we are defining function-like macro where an argument “n” is passed with “#n” which will allow you to retrieve string literal.

Conclusion

In C programming language, the macro is not less than similar to functions, but macro has built-in macro also. In C, the macro is used to define any constant value or any variable with its value in the entire program that will be replaced by this macro name, where macro contains the set of code that will be called when the macro name is used in the program. The macro uses the “#define” directive to define the macro in C programming language. This is very useful to save time and have less code writing.

Recommended Articles

This is a guide to Macros in C. Here we also discuss the introduction and working of macros in c along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Macros in C++ 
  2. fprintf() in C
  3. Unsigned Int in C
  4. Const Pointer in C

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More