#define in C
#define in C
In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the micro substitution.
The #define directive can use any of the basic data types present in the C standard. The #define preprocessor directive lets a programmer or a developer define the macros within the source code.
This macro definition will allow the constant value that should be declared for the usage. Macro definitions cannot be changed within the program’s code as one does with other variables, as macros are not variables.
The #define is usually used in syntax that created a constant that is used to represent numbers, strings, or other expressions.
The #define directive should not be enclosed with the semicolon(;). It is a common mistake done, and one should always treat this directive as any other header file. Enclosing it with a semicolon will generate an error.
The #define creates a macro, which is in association with an identifier or is parameterized identifier along with a token string. After the macro is defined, then the compiler can substitute the token string for each occurrence of the identifier within the source file.
Syntax:
#define NAME value /* this syntax creates a constant using define*/ Or #define NAME (expression) /* this syntax creates a constant using define*/ Where;
NAME: is the name of a particular constant. It can either be defined in smaller case or upper case or both. Most of the developers prefer the constant names to be in the upper case to find the differences.
value: defines the value of the constant.
Expression: is the value that is assigned to that constant which is defined. The expression should always be enclosed within the brackets if it has any operators.
E.g.:
#include <stdio.h> #include <conio.h> #define PI 3.14 int main() { printf("%f",PI); }
Output:
3.140000
#include <stdio.h> #define MIN(a,b) ((a)<(b)?(a):(b)) void main() { printf("The minimum value between 50 and 20 is: %d\n", MIN(50,20)); }
Output:
The minimum value between 50 and 20 is: 20
Number
The following is an example of how you use the #define directive to define a numeric constant:
E.g.:
#define AGE 10
In this example, the constant named AGE would contain the value of 10.
String
You can use the #define directive to define a string constant.
E.g:
#define NAME "TuotiralsAndExamples.com" In this example, the constant called NAME would contain the value of "TutorialsAndExamples.com". #include <stdio.h> #include <conio.h> #define NAME "TuotiralsAndExamples.com" #define AGE 10 int main() { printf("%s is over %d years old.\n", NAME, AGE); return 0; }
Output:
TuotiralsAndExamples.com is over 10 years old.
Expression
You can use the #define directive to define a constant using an expression.
E.g.:
#define AGE (20 / 2) In this example, the constant named AGE would also contain the value of 10. #include <stdio.h> #include <conio.h> #define AGE (20 / 2) int main() { printf("TuotiralsAndExamples is over %d years old.\n", AGE); return 0; }
Output:
TuotiralsAndExamples.com is over 10 years old.
The #define is a preprocessor directive allows you to specify a name and replacement text. As the preprocessor parses the source file, each occurrence of the name is replaced by its associated text. The scope of #define is limited to the file in which it is defined. So, #defines which are created in one source file are NOT available in a different source file.
Typically, #defines is shared between multiple files are stored in a header file (*.h) which is included in each source file that requires the #define.