Introduction
C language में #define का उपयोग symbolic constant बनाने के लिए किया जाता है।
जब program compile होने से पहले preprocessor source code को पढ़ता है, तब वह #define के through दिए गए नाम को उसकी value से replace कर देता है।
इसका मतलब यह है कि अगर आपने program में कई जगह एक ही fixed value use की है, तो उसे बार-बार लिखने की जगह एक नाम दे सकते हैं। इससे code अधिक साफ, readable और easy to maintain बनता है।
उदाहरण के लिए, अगर किसी program में बार-बार 3.14 लिखना पड़ रहा है, तो हम उसे PI नाम दे सकते हैं।
Syntax
#define constant_name value
Example:
#define PI 3.14
#define MAX 100
यहाँ:
PIकी जगह3.14MAXकी जगह100
automatically replace हो जाएगा।
Important Point
#define में:
- semicolon (
;) नहीं लगाया जाता - data type नहीं लिखा जाता
- यह compile होने से पहले काम करता है
Example Program 1: Circle ka Area
#include
<stdio.h>
#define PI 3.14int
main() {
float radius, area;
printf("Enter radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of circle = %.2f\n", area);
return 0;
}
Program Explanation
इस program में सबसे पहले:
#define PI 3.14
लिखा गया है। इसका अर्थ है कि program में जहाँ भी PI लिखा होगा, preprocessor उसे 3.14 से replace कर देगा।
Step by step working:
stdio.hheader file include की गई ताकिprintf()औरscanf()का उपयोग कर सकें।PIनाम का constant define किया गया।radiusऔरareaनाम के float variables बनाए गए।- user से radius input लिया गया।
- area निकालने का formula लगाया गया: यानी:
area = PI * radius * radius;
- result screen पर print कर दिया गया।
Suppose Input:
Enter radius: 5
Output:
Area of circle = 78.50
क्योंकि:
Example Program 2: Simple Interest
#include <stdio.h>
#define RATE 5
int main() {
float principal, time, si;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter time: ");
scanf("%f", &time);
si = (principal * RATE * time) / 100;
printf("Simple Interest = %.2f\n", si);
return 0;
}
Program Explanation
इस program में:
#define RATE 5
का उपयोग किया गया है। यहाँ RATE interest rate को represent कर रहा है।
Working:
- Principal amount user से लिया गया।
- Time input लिया गया।
- Simple Interest का formula लगाया गया:
- यहाँ
Rकी जगहRATEuse किया गया। - Program output में simple interest print करता है।
Suppose Input:
Enter principal amount: 2000
Enter time: 2
Output:
Simple Interest = 200.00
क्योंकि:
#define for Constants का Advantages
1. Code readable बनता है
numeric values की जगह meaningful name use होता है।
जैसे:
#define PI 3.14
यह 3.14 directly लिखने से बेहतर है।
2. Easy to modify
अगर value change करनी हो, तो केवल एक जगह change करना होगा।
3. Reusability
एक ही constant को program में कई जगह use कर सकते हैं।
4. Mistake कम होती है
बार-बार same value लिखने पर error होने की संभावना रहती है, लेकिन constant define करने से यह कम हो जाती है।
Limitations
- इसमें type checking नहीं होती
- यह सिर्फ text replacement करता है
- debugging कभी-कभी मुश्किल हो सकती है
Interview / Exam Point
याद रखने वाली बात:
#definesymbolic constants बनाने के लिए use होता है- यह preprocessor directive है
- इसमें semicolon नहीं लगाया जाता
- यह compile होने से पहले replace होता है
Short Difference: Constant Variable vs #define
| Point | #define Constant | const Variable |
|---|---|---|
| Type | No data type | Data type required |
| Process | Preprocessor replace करता है | Compiler handle करता है |
| Memory | Usually memory allocate नहीं होती | Memory allocate हो सकती है |
| Checking | Type checking नहीं | Type checking होती है |
Conclusion
#define for constants C language का बहुत useful feature है।
यह fixed values को meaningful names देने में मदद करता है, जिससे program simple, readable और maintainable बनता है।
Program में constants जैसे PI, RATE, MAX, MIN आदि को define करने के लिए इसका use बहुत common है।