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 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;
}
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 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;
}
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.
Also see, Tribonacci Series and Short int in C Programming
Frequently Asked Questions
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.
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.
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.
Refer to our guided paths on Code Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in contests hosted on Code Studio! But if you have just started your learning process and looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must have a look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blog if you find them helpful and engaging!
Happy Learning!