Introduction
C language में #define का उपयोग सिर्फ constants के लिए ही नहीं बल्कि functional macros (macro functions) बनाने के लिए भी किया जाता है।
ये macros functions की तरह दिखते हैं, लेकिन actually ये function call नहीं होते, बल्कि preprocessor द्वारा direct text replacement किए जाते हैं।
यानी:
- Function → Runtime पर execute होता है
- Macro → Compile होने से पहले replace हो जाता है
इसलिए macros fast होते हैं क्योंकि इनमें function call overhead नहीं होता।
Syntax
#define MACRO_NAME(parameters) expression
Example:
#define SQUARE(x) (x * x)
यहाँ SQUARE(5) लिखने पर preprocessor इसे (5 * 5) में बदल देगा।
Important Points
- Macro में data type define नहीं किया जाता
- यह function नहीं है, सिर्फ text replacement है
- हमेशा expression को brackets
( )में लिखना चाहिए - Compile होने से पहले ही replace हो जाता है
Example Program 1: Square निकालना
#include <stdio.h>
#define SQUARE(x) (x * x)
int main() {
int num = 5;
int result;
result = SQUARE(num);
printf("Square = %d\n", result);
return 0;
}
Program Explanation
इस program में:
#define SQUARE(x) (x * x)
define किया गया है।
Step-by-step:
SQUARE(num)call किया गया- Preprocessor इसे replace करता है:
result = (num * num);
num = 5है, इसलिए:
result = 5 * 5 = 25
- Output print होता है:
Square = 25
Example Program 2: Maximum of Two Numbers
#include <stdio.h>
#define MAX(a, b) (a > b ? a : b)
int main() {
int x = 10, y = 20;
printf("Maximum = %d\n", MAX(x, y));
return 0;
}
Program Explanation
यहाँ macro define किया गया है:
#define MAX(a, b) (a > b ? a : b)
यह ternary operator का उपयोग करके बड़ा number return करता है।
Working:
MAX(x, y)call किया गया- Preprocessor इसे replace करता है:
(x > y ? x : y)
- Values put होती हैं:
(10 > 20 ? 10 : 20)
- Condition false है, इसलिए result =
20
Output:
Maximum = 20
Important Warning (Common Mistake)
Macro में brackets का use बहुत जरूरी है।
Wrong:
#define SQUARE(x) x * x
Example Problem:
SQUARE(2 + 3)
Replace होगा:
2 + 3 * 2 + 3 = 11 (गलत)
Correct:
#define SQUARE(x) (x * x)
अब:
(2 + 3) * (2 + 3) = 25
Advantages of Functional Macros
1. Fast execution
Function call overhead नहीं होता
2. Code reusable
एक macro को कई जगह use कर सकते हैं
3. Simple logic के लिए useful
छोटे calculations में बहुत काम आता है
Limitations
- Type checking नहीं होती
- Debug करना मुश्किल होता है
- Complex expressions में errors हो सकते हैं
- Code unreadable हो सकता है
Macros vs Functions (Short View)
| Feature | Macro | Function |
|---|---|---|
| Execution | Compile time | Run time |
| Speed | Fast | Slow |
| Type Checking | No | Yes |
| Debugging | Hard | Easy |
Exam / Interview Points
- Functional macro एक parameterized macro होता है
- यह function नहीं होता, सिर्फ text replacement करता है
- हमेशा brackets use करें
- छोटे operations के लिए best है
Conclusion
#define for functional macros C का एक powerful feature है जो functions की तरह काम करता है लेकिन ज्यादा fast होता है।
यह छोटे calculations और repeated expressions के लिए बहुत useful है, लेकिन इसे carefully use करना चाहिए क्योंकि इसमें type checking और debugging की सुविधा नहीं होती।