C #define

In the C programming language, the #define directive is used to create preprocessor macros. These macros allow you to define constants or simple functions that are processed by the compiler before the actual compilation takes place.

The #define directive has the following syntax:

#define macro_name replacement_text

When the preprocessor encounters a #define directive, it replaces all occurrences of macro_name in the code with replacement_text. This substitution is done before the compilation starts, and it is purely a textual substitution. There is no type checking or evaluation involved.

Here are a few examples to illustrate its usage:

#define MAX_VALUE 100
#define SQUARE(x) ((x) * (x))

int main() {
    int x = MAX_VALUE;  // Replaced with 100
    int y = SQUARE(5);  // Replaced with ((5) * (5)), evaluates to 25

    return 0;

In the above example, MAX_VALUE is a constant macro that is replaced with 100 wherever it appears in the code. The SQUARE macro is a function-like macro that squares the given argument.

It’s important to note that #define macros are simple text substitutions and do not have any scope. They are global and visible throughout the code. Additionally, #define directives are typically placed at the top of the source file or in a header file for better organization and reusability.