Define for Constants

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.14
  • MAX की जगह 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:

  1. stdio.h header file include की गई ताकि printf() और scanf() का उपयोग कर सकें।
  2. PI नाम का constant define किया गया।
  3. radius और area नाम के float variables बनाए गए।
  4. user से radius input लिया गया।
  5. area निकालने का formula लगाया गया: Area=πr2Area = \pi r^2 यानी:
area = PI * radius * radius;
  1. result screen पर print कर दिया गया।

Suppose Input:

Enter radius: 5

Output:

Area of circle = 78.50

क्योंकि:3.14×5×5=78.503.14 \times 5 \times 5 = 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:

  1. Principal amount user से लिया गया।
  2. Time input लिया गया।
  3. Simple Interest का formula लगाया गया:

SI=P×R×T100SI = \frac{P \times R \times T}{100}

  1. यहाँ R की जगह RATE use किया गया।
  2. Program output में simple interest print करता है।

Suppose Input:

Enter principal amount: 2000
Enter time: 2

Output:

Simple Interest = 200.00

क्योंकि:SI=2000×5×2100=200SI = \frac{2000 \times 5 \times 2}{100} = 200

#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

याद रखने वाली बात:

  • #define symbolic constants बनाने के लिए use होता है
  • यह preprocessor directive है
  • इसमें semicolon नहीं लगाया जाता
  • यह compile होने से पहले replace होता है

Short Difference: Constant Variable vs #define

Point#define Constantconst Variable
TypeNo data typeData type required
ProcessPreprocessor replace करता हैCompiler handle करता है
MemoryUsually memory allocate नहीं होतीMemory allocate हो सकती है
CheckingType 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 है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top