🟢 Beginner · Lesson 07
Constants – #define, const
Constant क्या है?
Constant वो value है जो program execution के दौरान बदल नहीं सकती। Variable के विपरीत, constant define होने के बाद उसकी value हमेशा fixed रहती है।
- Readable —
PI,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 | #define | const |
|---|---|---|
| Processing | Preprocessor द्वारा | Compiler द्वारा |
| Data type | कोई type नहीं | Specific data type होता है |
| Memory | Memory allocate नहीं होती | Memory allocate होती है |
| Scope | Global (file के अंत तक) | Block scope rules follow करता है |
| Best for | Simple values, macros | Typed constants |
सारांश
- Constants = वो values जो program में बदलती नहीं
#define NAME value— preprocessor text substitution, no type, no semicolonconst type NAME = value;— typed constant, compiler द्वारा checkenum— related named integer constants का set, default 0 से शुरू- Convention: constants को ALL_CAPS में लिखें
- Type safety के लिए
#defineकी बजायconstprefer करें
अक्सर पूछे जाने वाले प्रश्न (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 में आकस्मिक बदलाव भी रोकता है जिन्हें स्थिर रहना चाहिए।💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.