


It is quick and efficient using this function of C which is also easy.
#Define skyrocket code#
The code also looks neat when the #define function is used. It is also efficient as you will just have to specify the variable name in the code. It is faster as the value is already defined before the code starts running. It helps in having uniformity for that variable. It can be the expression, variable, or any value which you would want to be constant. The #define function helps us in defining constant variables that can be used throughout the program. Below will be the output of the above code, which helps us in even defining constant functions. This function, whenever called, will print the variable, which is passed to the msg_for function. The function printing is message is defined using the #define function. Here we send the parameter as EduCBAians. The value of id sent from main by calling the msg_for function. We defined a variable name here, and this is printed using #. We can send parameters to the variable, which is passed and created through the #define a variable. The # operator here acts as an operator that helps us accept user input for a particular variable. In this example, we are making use of the ‘#’ operator. Printf(#a " : Let us learn something new!\n") The value for variable PUN will be replaced with the constant string that we have declared. The output of the above program will be as below. The variable to be printed in PUN was defined using the #define. In the main program, we have printed a string that displays the city you live in. It helps us in using this string value anywhere in the code. The above program is an example where we have defined a string variable PUN using the #define function. Printf("The city I live in is %s ", PUN) Replacing a string value using the #defineĬode: // C program to demonstrate #define to replace a string value Below will be the output of the above program. The value for this remains constant, and we get the area and circumference. Both the times we have used these variables. Once to calculate the area and the second time to calculate the circumference of the circle. We have used the MATHPI variable twice in the code. We can calculate the area and circumference of the circle by using the #define variable and the local variables. These variables will store the local value of variables. This value will remain constant throughout the program and can be used multiple times. Here we have used the define and defined the value of MATHPI as 3.1415. The above function helps us get the area of a circle by making use of a constant value derived by using the #define function. Printf("The circumference of circle is= %.2f",circum) Printf("The area of circle is= %.2f",area) Printf("Enter the radius for the circle: ") Replacing a numeric value using the #define function Given below are the examples of #Define in C: Example #1 This can be done for values whose value will not be changing. It will get replaced with its value wherever this function is used in the program. Its value can just be referred by the MATHPI variable name. Once this is defined, this function is stored in the memory and then can be directly used throughout the program. It helps us in having a constant value for this MATHPI constant variable. Like #include, we have used #define and declared its value as 3.14. In the above example, we try to understand the working of #define function. How does C# directive work in C? #include Īs stated earlier, the program helps us in creating constant values that can be used directly. This directive can be used throughout the program as and when needed. The argument after the CONSTNAME can be tokens, values for constants, and also complete statements.

The CONSTNAME is replaced only when it forms a token. The directive #define helps in creating an identifier that will be a constant value.
