🟢 Beginner  ·  Lesson 07

Constants – #define, const

Constant क्या है?

Constant वो value है जो program execution के दौरान बदल नहीं सकती। Variable के विपरीत, constant define होने के बाद उसकी value हमेशा fixed रहती है।

  • ReadablePI, 3.14159 से ज़्यादा clear है
  • Maintain करने में आसान — एक जगह value बदलो, पूरा program update
  • Safe — constant modify करने पर compiler error देता है

#define – Preprocessor Macro

#define directive symbolic constant बनाता है। Preprocessor compilation से पहले हर occurrence को value से replace करता है।

Syntax
#define CONSTANT_NAME value
// ध्यान दें: कोई semicolon नहीं! कोई data type नहीं! कोई = नहीं!
C Language
#include <stdio.h>

#define PI       3.14159
#define MAX_SIZE 100
#define SCHOOL   "Alpine Public School"

int main() {
    float r = 7.0f;
    printf("Kshetrafal = %.2f\n", PI * r * r);
    printf("School: %s\n", SCHOOL);
    printf("Max size: %d\n", MAX_SIZE);
    return 0;
}
Kshetrafal = 153.94 School: Alpine Public School Max size: 100
💡 Naming Convention

Constants को ALL_CAPS में लिखते हैं (underscore के साथ)। इससे वो variables से easily अलग दिखते हैं।

const Keyword

const keyword एक variable को read-only बना देता है। #define के विपरीत, इसका data type होता है।

C Language
const float PI = 3.14159f;
const int   MAX = 100;
const char  GRADE = 'A';

printf("PI = %.5f\n", PI);

// PI = 3.14;  // ERROR! const को modify नहीं कर सकते

enum – Named Constants

enum related named integer constants का set बनाता है। Magic numbers की जगह meaningful names use करने से code readable बनता है।

C Language
enum Din { SOM, MANGAL, BUDH, BRIHASPATI, SHUKRA, SHANIV, RAVI };
// SOM=0, MANGAL=1, BUDH=2, SHUKRA=4, ...

enum Traffic { LAL, PEELA, HARA };

int main() {
    enum Din aaj = SHUKRA;
    printf("Shukravar = %d\n", aaj);     // 4

    enum Traffic signal = HARA;
    if (signal == HARA) printf("Jao!\n");
    return 0;
}
Shukravar = 4 Jao!

#define vs const में अंतर

Feature#defineconst
ProcessingPreprocessor द्वाराCompiler द्वारा
Data typeकोई type नहींSpecific data type होता है
MemoryMemory allocate नहीं होतीMemory allocate होती है
ScopeGlobal (file के अंत तक)Block scope rules follow करता है
Best forSimple values, macrosTyped constants

सारांश

  • Constants = वो values जो program में बदलती नहीं
  • #define NAME value — preprocessor text substitution, no type, no semicolon
  • const type NAME = value; — typed constant, compiler द्वारा check
  • enum — related named integer constants का set, default 0 से शुरू
  • Convention: constants को ALL_CAPS में लिखें
  • Type safety के लिए #define की बजाय const prefer करें

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में constant क्या है?
Constant एक ऐसी value है जो program चलते समय बदली नहीं जा सकती। एक बार set होने पर उसे बदलने की कोई भी कोशिश error है। Constants सप्ताह के दिनों की संख्या या pi के मान जैसी values को आकस्मिक बदलाव से बचाकर code को सुरक्षित और स्पष्ट बनाते हैं।
C में #define और const में क्या अंतर है?
#define एक preprocessor macro बनाता है जो compilation से पहले बस text-substitute होता है और उसका कोई type नहीं। const एक असली typed variable बनाता है जो बदला नहीं जा सकता, तो compiler उसका type जाँचता है और debugging में उसे देखा जा सकता है। Typed constants के लिए आमतौर पर const पसंद किया जाता है।
C में constant कैसे declare करते हैं?
दो आम तरीके: #define PI 3.14159 को preprocessor macro के रूप में इस्तेमाल करें, या const keyword से variable declare करें जैसे const int DAYS = 7;। दोनों आपको एक ऐसी value देते हैं जो program के दौरान बदलनी नहीं चाहिए।
C में symbolic constant क्या है?
Symbolic constant किसी निश्चित value को दिया गया नाम है, आमतौर पर #define से, ताकि आप अपने पूरे code में raw number के बजाय अर्थपूर्ण नाम लिख सकें। तब value बदलने का मतलब सिर्फ़ एक line संपादित करना है, जो errors कम करता है।
Literal values के बजाय constants क्यों इस्तेमाल करें?
नामित constant code को पढ़ना और रखरखाव आसान बनाता है: MAX_USERS जैसा नाम अपना अर्थ समझाता है, और value बदलनी हो तो आप एक ही जगह update करते हैं। यह उन values में आकस्मिक बदलाव भी रोकता है जिन्हें स्थिर रहना चाहिए।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.