Table of contents
1.
Introduction
2.
What is #define in C?
3.
Why We Need #define?
4.
Syntax
5.
Examples 
5.1.
Example 1: 
5.2.
C
5.3.
Example 2: 
5.4.
C
6.
Uses
7.
Frequently Asked Questions
7.1.
Why is #define used?
7.2.
How can we define a macro in the C programming language?
7.3.
Can we change the value of #define in our code?
7.4.
What are the 4 conditional statements in C?
7.5.
What is define in C++?
8.
Conclusion
Last Updated: Feb 18, 2025
Medium

C #define

Author ANKIT KUMAR
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the C programming language, all the statements starting with the # (hash) symbol are known as preprocessor directives/commands. The #define is also a preprocessor directive. Preprocessor directives are executed before any other command in our program. 

C #define

In a C Program, we generally write all the preprocessor directives outside the main() function at the top of our C program. #define directive is used to define constants or an expression in our C program.

Before we move on to learn the syntax of the #define preprocessor directive, let us first see why we even need it.

Recommended Topics, Sum of Digits in C

What is #define in C?

#define is a preprocessor directive used in C programs to define macros. The #define directive is used to declare constant values or a named expression that can be used throughout our C program. The preprocessor command #define is used in C programming to define symbolic constants and macros. By giving a constant variable or a section of code a name, it can improve the readability, flexibility, and maintainability of the code.

There are multiple use cases of #define in C:

Defining constants 

#define PI 3.14159

 
Defining macros 

#define SQUARE(x) ((x) * (x))

Why We Need #define?

#define is used to define macros in C. In very simple terms, consider a situation where a particular value is repeated very often in the program, and the value does not change. In most cases, we make a constant variable with the help of the const keyword and put that value in the declared variable. Now we can use this variable everywhere in our program.

The #define preprocessor directive is very similar to the const. However, not only can we define a constant, we can define an expression too. Macro definitions are not variables and cannot be changed by your program code like variables.

Let us first see the syntax of #define, and then we shall see a few examples that will help us understand more about #define.

Syntax

For Constants

#define CONST_NAME value

 

For an expression

#define CONST_NAME (expression within brackets)

 

  • CONST_NAME: This represents the name of the constant. By convention, it is all in capital letters. Wherever in the program the #define directive (CONSt_NAME) will be encountered, it will get replaced with the value assigned to it.
  • Value: It represents the value assigned to the constants defined using #define.
  • Expression: It is similar to any expression in C. It must always be enclosed within brackets.

It is important to note that the entire #define statement does not use the semicolon(;) symbol to end the statement.

Examples 

Example 1: 

Using #define to define a constant

  • C

C

// C program to demonstrate #deine 
#include<stdio.h>


// The #define preprocessor directive
// This defines a constant NAME with value as Coding Ninjas
// We do not write '=' after NAME to assign value to it.
// We do not use ';' at the end of the statement
#define NAME "Coding Ninjas"

int main()
{
   // We use the value of NAME
   // It is not declared in the main() function, still we can access it
   printf("I took a course from %s", NAME);
   return 0;
}
You can also try this code with Online C Compiler
Run Code

Output: 

I took a course from Coding Ninjas

Key Points

  • We do not write '=' after NAME(the constant) to assign value to it
  • We do not use ';' at the end of the statement as we do in another part of the code.
  • We do not specify the data type while using #define to define the constants.
     

Example 2: 

Using #define to define an expression

  • C

C

// C program to demonstrate #deine for expressions 
#include<stdio.h>


// The #define preprocessor directive
// This defines a expression AREA which takes two values as parameter
// and returns the area
// We do not write '=' after AREA to assign value to it.
// We do not use ';' at the end of the statement
#define AREA(l,b) (l*b)

int main()
{
   // we can simply use the AREA(l,b) as a normal function
   // AREA(5,8) returns 40
   int area1 = AREA(5,8);
  
   // AREA(40,7) returns 280
   int area2 = AREA(40,7);
  
   printf("The first area is = %d", area1);
   printf("\nThe second area is = %d", area2);
  
   return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

The first area is = 40
The second area is = 280

 

Key Points

  • We do not write '=' after AREA to assign value to it.
  • We do not use ';' at the end of the statement.

Uses

  • Conditional Compilation: Control which code gets compiled based on defined symbols.
  • Platform-Specific Code: Include code tailored for specific operating systems or hardware platforms.
  • Feature Toggles: Enable or disable specific features or functionalities for different builds.
  • Debugging and Testing: Include debugging or testing code only in development or test builds.
  • Version Control: Manage different versions of the application by including or excluding code sections based on version symbols.
  • Resource Optimization: Optimize performance or memory usage by including/excluding resource-intensive code based on build requirements.
     

Also see, Tribonacci Series and Short int in C Programming

Frequently Asked Questions

Why is #define used?

The #define command is part of the C  preprocessor languages. When used in code, the compiler simply replaces the #define statement with whatever you specify. It can be useful in doing tasks that are repeated in the code. 

For e.g , by using #define fori(x) for (int i=0; i<x; i + +), we can simply write fori(10) to iterate from i=0 to 9, instead of typing the entire code for the for loop.

How can we define a macro in the C programming language?

In C, we can define a macro using the #define preprocessor directive.

For e.g., #define PI 3.14

Can we change the value of #define in our code?

No, we cannot change the value of macro defined using #define in our code as it is not like a normal variable. It can only be changed at the place of definition.

What are the 4 conditional statements in C?

In C conditional statements are used for decision making. Some of the most used conditional statements in C are if statement, if-else statement, else-if statement,  and switch statement. 

What is define in C++?

In C++, #define is a preprocessor directive used to define constant values or macros that can be used throughout the code. It replaces occurrences of a specified identifier with a given value or expression before the actual compilation begins. This helps in managing constant values or repetitive code without having to manually modify every instance.

Conclusion

In this article, we have extensively discussed #define in C programming language along with the code examples to get a better understanding of #define in C. Learn more about functions in C here. You can explore all the blogs related to C here.

Live masterclass