top of page

What is a Constant? / कॉन्स्टैंट क्या है?

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Oct 6, 2025
  • 2 min read
  • A constant is a fixed value that cannot be changed during the execution of a program. / कॉन्स्टैंट एक ऐसा मान होता है जो प्रोग्राम रन होते समय बदला नहीं जा सकता।

  • Unlike variables, constants hold the same value throughout the program. / वेरिएबल के विपरीत, कॉन्स्टैंट की वैल्यू प्रोग्राम के दौरान स्थिर रहती है।



Types of Constants / कॉन्स्टैंट के प्रकार

  1. Integer Constants / पूर्णांक कॉन्स्टैंट

    • Whole numbers without decimal points, can be decimal (normal), octal (leading 0), or hexadecimal (leading 0x). / कोई दशमलव नहीं, जैसे 10, 012 (octal), 0xA (hex).

    • Examples: 10, 075, 0x1A, 100u, 50UL

  2. Floating Point Constants / दशमलव कॉन्स्टैंट

    • Numbers with a decimal point or in exponential form. / दशमलव या Exponential जैसे 3.14, 6.02e23.

    • Examples: 3.14, 6.022e23, .25, 1E-4

  3. Character Constants / कैरेक्टर कॉन्स्टैंट

    • Single characters enclosed in single quotes. / एक अक्षर या सिम्बल एकल उद्धरण के भीतर।

    • Examples: 'A', '5', '+', escape sequences भी आते हैं जैसे '\n' (newline).

  4. String Constants / स्ट्रिंग कॉन्स्टैंट

    • Sequence of characters enclosed in double quotes. / दोहरे उद्धरण में टेक्स्ट की श्रृंखला।

    • Example: "Hello, World!", "12345"

  5. Enumeration Constants / इन्न्यूमरेशन कॉन्स्टैंट

    • Named constants defined using enum. / enum के द्वारा नामित स्थिर मान।

    • Example:

      enum days { Sunday, Monday, Tuesday };

    • Sunday = 0, Monday = 1, and so on automatically assigned.


How to Create Constants in C / C में कॉन्स्टैंट कैसे बनाएं

Using const keyword / const कीवर्ड


const int MAX = 100;

  • This creates a variable MAX whose value cannot be changed. Any attempt to modify will give a compile error. / MAX को बदला नहीं जा सकता।

Example:


const float PI = 3.14;

PI = 3.1415;  // Error: assignment of read-only variable ‘PI’

Using #define preprocessor / #define प्रीप्रोसेसर


#define PI 3.14

  • This creates a symbolic constant. Wherever PI appears, compiler replaces it with 3.14. / जैसे मैक्रो की तरह काम करता है।

  • No memory allocated for #define, substitution at compile-time.

Example:


#define MAX_AGE 120


Rules for Constants / कॉन्स्टैंट के नियम

  • Constants must be assigned a value at the time of declaration. / घोषणा के समय मान देना ज़रूरी।

  • Literal constants cannot change during program execution. / Literal का मान change नहीं हो सकता।

  • Constants help make programs readable and easier to maintain. / कोड को पढ़ने में आसान बनाते हैं।


Examples for Different Constants / विभिन्न कॉन्स्टैंट्स के उदाहरण


#include <stdio.h>

#define PI 3.14159    // Macro constant

int main() {
    const int DAYS_IN_WEEK = 7; // const constant declaration

    printf("Value of PI: %f\n", PI);
    printf("Days in a week: %d\n", DAYS_IN_WEEK);

    // DAYS_IN_WEEK = 8; // Error: cannot modify a const variable

    char newline = '\n'; // Character constant (escape sequence)

    printf("Hello%cWorld\n", newline);

    return 0;
}

Summary (सारांश)

  • Constants are fixed values that do not change during program execution. / स्थिर मान जो बदलते नहीं।

  • Use const keyword for type-safe constants and #define for symbolic constants. / type-safe के लिए const, symbolic के लिए #define.

  • Helps in avoiding accidental modification and improves code clarity. / गलत बदलाव से बचाता है और कोड स्पष्ट बनाता है।

अगर कॉन्स्टैंट्स के types या use cases पर और उदाहरण चाहिए तो बताएं।

 
 
 

Comments


bottom of page