Notes in Hindi

What are Constants in C Language in Hindi

/ BCA / Programming with C and CPP

Constants in C Language in Hindi

What are Constants in C Language in Hindi

C भाषा में Constants उन मानों (values) को कहा जाता है जो प्रोग्राम के चलते समय कभी नहीं बदलते। मतलब एक बार अगर आपने किसी Constant को किसी value के साथ define कर दिया, तो पूरे प्रोग्राम में वह value same ही रहेगी।

जब हम कोई ऐसा डेटा इस्तेमाल करते हैं जो पूरे प्रोग्राम में fix रहना चाहिए, जैसे PI का मान 3.14 या किसी subject की maximum marks, तब हम उसे Constant बनाते हैं। इससे ना केवल हमारा code सुरक्षित रहता है, बल्कि accidental changes से भी बचा रहता है।

Importance of Constants

  • Constants को use करने से program में गलती की संभावना कम हो जाती है।
  • Code अधिक readable और maintainable बनता है।
  • Fixed values को बार-बार लिखने की बजाय एक ही जगह define करना efficient होता है।

Difference between Variables and Constants in Hindi

Variables और Constants दोनों ही डेटा को स्टोर करने के लिए इस्तेमाल किए जाते हैं, लेकिन इनमें एक बड़ा अंतर होता है:

Aspect Variables Constants
Value Change हो सकती है Fixed रहती है
Declaration सामान्य तरीके से const या #define के साथ
Use Dynamic data के लिए Static data के लिए
Example int age = 25; const int MAX = 100;

Types of Constants in C Language in Hindi

C भाषा में कई प्रकार के Constants होते हैं जो अलग-अलग प्रकार के data को represent करते हैं। नीचे हम सभी मुख्य प्रकार को विस्तार से समझ रहे हैं:

1. Integer Constants

  • ये whole numbers होते हैं जैसे 10, -5, 1000
  • इनमें कोई decimal point नहीं होता
  • Example: const int num = 100;

2. Floating Point Constants

  • ये decimal वाले numbers होते हैं जैसे 3.14, -0.98
  • Real values को represent करते हैं
  • Example: const float pi = 3.14;

3. Character Constants

  • एक character को single quotes में represent किया जाता है
  • Example: const char grade = 'A';

4. String Constants

  • String मतलब characters का group, जो double quotes में होता है
  • Example: const char name[] = "Lala";

5. Enumeration Constants

  • यह user-defined constants होते हैं जो related values को group करते हैं
  • Example:
enum days {SUN, MON, TUE, WED};

Declaring Constants using const and hash define in Hindi

C भाषा में Constants declare करने के दो मुख्य तरीके होते हैं:

1. Using const keyword

  • const एक keyword है जिससे हम एक variable को constant बना सकते हैं।
  • इससे बनाए गए variable की value बदली नहीं जा सकती।

Syntax:

const data_type variable_name = value;

Example:

const float pi = 3.1416;
const int maxMarks = 100;

2. Using #define preprocessor

  • #define एक preprocessor directive है जो compile time पर value को replace कर देता है।
  • इसमें semicolon (;) नहीं लगाया जाता।
  • यह variables की तरह नहीं बल्कि macro की तरह काम करता है।

Syntax:

#define NAME value

Example:

#define PI 3.1416
#define MAX 100

const vs #define

Aspect const #define
Type Safety Type checking होती है Type checking नहीं होती
Scope Block या function में सीमित किया जा सकता है Global रहता है
Debugging Debug करना आसान Debugging में कठिनाई
Usage Variable की तरह Text replacement की तरह

जब किसे Use करना चाहिए?

  • अगर आपको compile time पर value fix करनी है और type checking चाहिए, तो const इस्तेमाल करें।
  • अगर सिर्फ simple macro चाहिए जिसका नाम code में multiple बार use होगा, तो #define बेहतर है।

Example Program using const and #define

#include <stdio.h>
#define PI 3.1416

int main() {
   const int radius = 5;
   float area;
   area = PI * radius * radius;
   printf("Circle ka Area: %.2f", area);
   return 0;
}

इस program में हमने PI को #define से और radius को const से define किया है, जिससे circle का area calculate किया गया।

FAQs

Constant वह मान होता है जो प्रोग्राम के execution के दौरान नहीं बदलता। इसे एक बार define करने के बाद उसकी value fix रहती है, जैसे const int max = 100;
Variable की value program के दौरान बदली जा सकती है, लेकिन Constant की value fix रहती है। Variable को general data store करने के लिए use किया जाता है जबकि Constant को fixed value store करने के लिए।
C में मुख्य रूप से 5 प्रकार के Constants होते हैं: Integer Constant, Floating Constant, Character Constant, String Constant, और Enumeration Constant।
const keyword का उपयोग किसी variable को Constant बनाने के लिए किया जाता है, जिससे उसकी value को बदला नहीं जा सकता। जैसे const float pi = 3.14;
const type-safe होता है और compile time पर check किया जाता है, जबकि #define सिर्फ text replacement करता है। const का scope limit किया जा सकता है, लेकिन #define global होता है।

Please Give Us Feedback