MCAWALA

C Programming Tutorial

C Programming में Constants क्या होते हैं? | Constants in C Language

C programming सीखते समय आपको सबसे पहले variables और constants समझना जरूरी होता है। आज हम इस ब्लॉग में बात करेंगे Constants के बारे में — क्या होते हैं, क्यों जरूरी होते हैं, उनके प्रकार, और कैसे इस्तेमाल करते हैं।

Constants क्या होते हैं? (What are Constants?)

Constants, या हिंदी में स्थिरांक, ऐसे values होते हैं जो प्रोग्राम के execution के दौरान बदलते नहीं हैं। मतलब, अगर आप एक बार किसी constant को define कर देते हो, तो उसका value कभी भी change नहीं किया जा सकता।

उदाहरण के लिए:

  • पाई (π) का मान लगभग 3.14 होता है, ये value हमेशा same रहती है।
  • गुरुत्वाकर्षण का acceleration लगभग 9.8 m/s² होता है, जो constant होता है।

इसलिए जब हमें कोई fixed या permanent value प्रोग्राम में इस्तेमाल करनी होती है, तो हम constants का use करते हैं।

Variables और Constants में फर्क (Difference between Variables and Constants)

Variables (चर) Constants (स्थिरांक)
Values बदल सकते हैं। Values एक बार set करने पर नहीं बदलते।
डेटा को store करने के लिए use होते हैं। Fixed values को store करने के लिए।
Example: int age = 20; Example: const float PI = 3.14;

C में Constants के प्रकार (Types of Constants in C)

C में मुख्य रूप से 4 तरह के constants होते हैं:

1. Integer Constants (पूर्णांक स्थिरांक)

ये ऐसे numbers होते हैं जो decimal, octal या hexadecimal form में हो सकते हैं।

  • Decimal: 10, 20, -15
  • Octal (0 से शुरू): 075 (octal)
  • Hexadecimal (0x से शुरू): 0x1A, 0xFF

2. Floating-point Constants (दशमलव स्थिरांक)

ये decimal वाले numbers होते हैं जैसे 3.14, 0.001, -12.56

3. Character Constants (चर स्थिरांक)

ये single character होते हैं और single quotes में लिखे जाते हैं, जैसे 'A', 'z', '5', '#'

4. String Constants (स्ट्रिंग स्थिरांक)

Character के समूह होते हैं, double quotes में लिखे जाते हैं जैसे "Hello World", "C Programming"

Constants को Define करने के तरीके (How to Define Constants in C)

1. #define Preprocessor Directive का Use

यह एक macro होता है जिसे प्रीप्रोसेसर compile से पहले replace कर देता है। इसकी syntax है:

#define CONSTANT_NAME value

Example:

#define PI 3.14
#define MAX_AGE 100

जब भी आप PI या MAX_AGE लिखेंगे, compiler इसे 3.14 और 100 से बदल देगा।

2. const Keyword का Use

यह C का keyword है जो एक variable को constant बनाता है। इसे declare करते समय value set करनी होती है और बाद में change नहीं कर सकते।

const int maxMarks = 100;
const float gravity = 9.8;

Constants का क्यों Use करें? (Why Use Constants?)

  • Code readability बढ़ती है: जब आप किसी value को constant नाम देते हैं, तो code पढ़ने में आसान हो जाता है।
  • Code maintain करना आसान होता है: अगर value को future में बदलना हो, तो सिर्फ एक जगह change करना पड़ता है।
  • गलती की संभावना कम होती है: बार-बार same value लिखने से error हो सकती है, constants से वो बचा जा सकता है।
  • Memory optimization: कुछ cases में constants compiler को optimize करने में मदद करते हैं।

Constants के साथ एक आसान उदाहरण (Simple Example with Constants)

#include <stdio.h>

#define PI 3.14  // Define constant using #define

int main() {
    float radius, area;

    printf("Circle का radius enter करें: ");
    scanf("%f", &radius);

    area = PI * radius * radius;  // Use constant PI
    printf("Circle का area है: %.2f\n", area);

    return 0;
}

इस example में, PI एक constant है जिसकी value हमेशा 3.14 रहेगी।

const Keyword का Example

#include <stdio.h>

int main() {
    const int MAX_AGE = 100;
    int age;

    printf("अपनी age enter करें: ");
    scanf("%d", &age);

    if(age > MAX_AGE) {
        printf("Invalid age\n");
    } else {
        printf("आपकी age है %d\n", age);
    }

    // MAX_AGE = 120; // यह error देगा क्योंकि MAX_AGE constant है।

    return 0;
}

Important Points (महत्वपूर्ण बातें)

  • const और #define दोनों constants बनाते हैं लेकिन उनका काम अलग होता है।
  • const type-safe होता है, जबकि #define सिर्फ text replacement करता है।
  • Constants को बड़े अक्षरों (UPPERCASE) में लिखना अच्छा practice माना जाता है जैसे MAX_AGE, PI ताकि आसानी से पहचाना जा सके।
  • Constants को initialize करना जरूरी होता है, बिना initialize किए constants नहीं बनाए जा सकते।

कुछ Common Constants जो प्रोग्रामिंग में Use होते हैं

Constant Name Value Description
PI 3.14 Circle का π
MAX_SIZE 1000 Array या Data का max size
TRUE 1 Boolean True
FALSE 0 Boolean False

Constants के फायदे (Advantages)

  • बग कम होते हैं: क्योंकि constant values accidentally change नहीं हो पातीं।
  • Code ज्यादा readable होता है।
  • सिर्फ एक जगह बदलने से पूरे प्रोग्राम में apply हो जाता है।
  • Optimization: Compiler constants को बेहतर optimize कर सकता है।

निष्कर्ष (Conclusion)

C programming में Constants का सही उपयोग बहुत जरूरी है। ये प्रोग्राम को सुरक्षित, साफ और maintainable बनाते हैं। चाहे आप #define का use करें या const keyword का, दोनों ही constants का अपना महत्व है।

नए programmers को यही सलाह दी जाती है कि constants का use करें, magic numbers (जैसे 3.14, 100 आदि सीधे कोड में लिखना) से बचें। इससे आपका code error-free और professional लगेगा।

अभ्यास (Practice)

  • खुद से कुछ constants define करें और उनका उपयोग करें।
  • प्रोग्राम लिखें जो circle, rectangle, या triangle के area calculate करे जहाँ π, maximum size आदि constants हों।
  • const और #define दोनों का उपयोग कर comparison करें।

आपके C programming के सफर के लिए ये constants की जानकारी बहुत मददगार साबित होगी। अगर आप और सीखना चाहते हैं तो हमारे दूसरे ब्लॉग भी जरूर पढ़ें।