Updated March 28, 2023
Introduction to #Define in C
The #define is a function in the C programming language that helps define macros along with the source code present. Using macro definitions, we can define constant values, which can be used globally throughout the code we have. These macro definitions are different than variables. They cannot be changed like variables can be changed in a program. Macros can be used to create some expressions of strings or even numbers. Macros are efficient, reusable, and fast. It is a way of creating constants.
Syntax
#define CONSTNAME value
Or
#define CONSTNAME expression
The directive #define helps in creating an identifier that will be a constant value. The CONSTNAME is replaced only when it forms a token. The argument after the CONSTNAME can be tokens, values for constants, and also complete statements. This directive can be used throughout the program as and when needed.
How does C# directive work in C?
#include <stdio.h>
#define MATHPI 3.14
main() {
printf("%f",MATHPI);
}
As stated earlier, the program helps us in creating constant values that can be used directly. In the above example, we try to understand the working of #define function. Like #include, we have used #define and declared its value as 3.14. It helps us in having a constant value for this MATHPI constant variable. Once this is defined, this function is stored in the memory and then can be directly used throughout the program. Its value can just be referred by the MATHPI variable name. It will get replaced with its value wherever this function is used in the program. This can be done for values whose value will not be changing.
Examples of #Define in C
Given below are the examples of #Define in C:
Example #1
Replacing a numeric value using the #define function
Code:
#include <stdio.h>
#define MATHPI 3.1415
int main()
{
float radius, area, circum;
printf("Enter the radius for the circle: ");
scanf("%f", &radius);
area = MATHPI*radius*radius;
printf("The area of circle is= %.2f",area);
return 0;
circum = 2*MATHPI*radius;
printf("The circumference of circle is= %.2f",circum);
}
The above function helps us get the area of a circle by making use of a constant value derived by using the #define function. Here we have used the define and defined the value of MATHPI as 3.1415. This value will remain constant throughout the program and can be used multiple times. We have taken three float variables. These variables will store the local value of variables. We can calculate the area and circumference of the circle by using the #define variable and the local variables. We have used the MATHPI variable twice in the code. Once to calculate the area and the second time to calculate the circumference of the circle. Both the times we have used these variables. The value for this remains constant, and we get the area and circumference. Below will be the output of the above program.
Output:
Example #2
Replacing a string value using the #define
Code:
// C program to demonstrate #define to replace a string value
#include <stdio.h>
// We have defined a string PUN for Pune
#define PUN "Pune"
int main()
{
printf("The city I live in is %s ", PUN);
return 0;
}
The above program is an example where we have defined a string variable PUN using the #define function. It helps us in using this string value anywhere in the code. In the main program, we have printed a string that displays the city you live in. The variable to be printed in PUN was defined using the #define. It cannot be changed further. The output of the above program will be as below. The value for variable PUN will be replaced with the constant string that we have declared.
Output:
Example #3
Defining an expression using #define
Code:
#include <stdio.h>
#define MAX(x,y) ((x)>(y)?(x):(y))
void main() {
printf("The maximum by using #define function is: %d\n", MAX(97,23));
}
The above program demonstrates the use of the #define function as an expression. We have defined an expression that is helping us in finding the maximum between two numbers. The expression MAX is defined with the logic of finding the maximum between two numbers. This #define variable once has this value of finding the max of two numbers. In the main function, we are just using a print function that helps us find the max of any two numbers. In the print function, we have just called the MAX function. We have passed two numbers to this MAX variable, which is defined as the maximum of two numbers. The output of this code will be as below.
Output:
Example #4
Use of ‘#’ when define function is used
Code:
#include <stdio.h>
#define msg_for(a) \
printf(#a " : Let us learn something new!\n")
int main(void) {
msg_for(EduCBAians);
return 0;
}
In this example, we are making use of the ‘#’ operator. The # operator here acts as an operator that helps us accept user input for a particular variable. We can send parameters to the variable, which is passed and created through the #define a variable. We defined a variable name here, and this is printed using #. The value of id sent from main by calling the msg_for function. Here we send the parameter as EduCBAians. The function printing is message is defined using the #define function. This is a constant function. This function, whenever called, will print the variable, which is passed to the msg_for function. Below will be the output of the above code, which helps us in even defining constant functions.
Output:
Conclusion
The #define function helps us in defining constant variables that can be used throughout the program. This value will remain constant. It can be the expression, variable, or any value which you would want to be constant. It helps in having uniformity for that variable. It is faster as the value is already defined before the code starts running. It is also efficient as you will just have to specify the variable name in the code. The code also looks neat when the #define function is used. It is quick and efficient using this function of C which is also easy.
Recommended Articles
This is a guide to #Define in C. Here we discuss How does C# directive work in C and Examples along with codes and outputs. You may also have a look at the following articles to learn more –