What is the #define Directive in C?
The #define directive in C is used to define symbolic constants or macros. When the preprocessor encounters this directive, it replaces all instances of the defined name with the specified value or code snippet before the compilation phase begins. This helps improve code readability, maintainability, and flexibility.
Syntax of #define
#define MACRO_NAME replacement_text
- MACRO_NAME: The name of the macro.
- replacement_text: The value or expression it will be replaced with.
For function-like macros:
#define MACRO_NAME(parameters) replacement_text
Types of Macros
1. Object-like Macros
These macros behave like constants and are typically used to define fixed values:
#define PI 3.14159 #define MAX_SIZE 100
Here, every occurrence of PI and MAX_SIZE in the code will be replaced with 3.14159 and 100, respectively.
2. Function-like Macros
These macros accept parameters and are used to define inline code fragments:
#define SQUARE(x) ((x) * (x)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
These macros can make code more concise but require careful usage to avoid unexpected behaviors due to multiple evaluations of parameters.
Potential Pitfall Example:
#define INCREMENT(x) (x + 1) int a = 5; int b = INCREMENT(a++); // May lead to unexpected behavior due to multiple evaluations
3. Conditional Macros
The #define directive works with conditional compilation commands to include or skip parts of the code based on certain conditions.
#define DEBUG #ifdef DEBUG printf("Debug mode ON\n"); #endif
Practical Examples
Example 1: Simple Constant Macro
#include <stdio.h> #define SANFOUNDRY_SCORE 100 int main() { printf("Score in Sanfoundry test = %d\n", SANFOUNDRY_SCORE); return 0; }
Output:
Score in Sanfoundry test = 100
This C program defines a constant using a macro. The line #define SANFOUNDRY_SCORE 100 tells the compiler to replace SANFOUNDRY_SCORE with 100 before compiling. In the main() function, it prints the message: “Score in Sanfoundry test = 100”.
Example 2: Macro with Parameters
#include <stdio.h> #define SQUARE(x) ((x) * (x)) int main() { int number = 5; printf("Square of number = %d\n", SQUARE(number)); return 0; }
Output:
Square of number = 25
Note: Always wrap macro arguments and body in parentheses to avoid operator precedence bugs.
Example 3: Macros as Code Snippets
#include <stdio.h> #define PRINT_PASS printf("Sanfoundry Certification: Passed\n"); int main() { int result = 90; if (result >= 60) PRINT_PASS return 0; }
Output:
Sanfoundry Certification: Passed
This C program uses a macro to simplify printing a message. The line #define PRINT_PASS printf(“Sanfoundry Certification: Passed\n”); defines a macro called PRINT_PASS. In the main() function, the variable result is set to 90. Since 90 is greater than or equal to 60, the if condition is true, so the macro runs and prints: “Sanfoundry Certification: Passed”.
Conditional Compilation with #define
The #define directive works in tandem with conditional compilation directives to include or exclude code segments based on certain conditions.
#define DEBUG #ifdef DEBUG printf("Debugging is enabled\n"); #endif
The printf will only be included if DEBUG is defined. This is useful for debugging or platform-specific features.
Advantages of Using #define
- Improved Readability: Using meaningful names for constants and code fragments makes the code easier to understand.
- Ease of Maintenance: Changes to macro definitions automatically propagate throughout the code, reducing the risk of errors.
- Performance Optimization: Function-like macros can eliminate the overhead of function calls by inserting code directly.
- Conditional Compilation: Macros can control the inclusion or exclusion of code blocks, facilitating platform-specific code and debugging.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for C Internship
- Practice BCA MCQs
- Check Computer Science Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos