Constants and Literals in C++
Constants vs literals
Literal वह value है जो source code में सीधे लिखी होती है: 100, 3.14, 'A', "Priya", true। Constant एक *नाम वाली* value है जो एक बार set होने के बाद बदली नहीं जा सकती, जैसे const int MAX_MARKS = 100;। Constants literals को अर्थ देते हैं और उन्हें गलती से बदलने से बचाते हैं।
Literals के प्रकार
| प्रकार | उदाहरण | Type |
|---|---|---|
| Integer | 42, -7, 1000000 | int (ज़रूरत हो तो बड़ा) |
| दूसरे base में integer | 0x2A (hex), 052 (octal), 0b101010 (binary, C++14) | int |
| Integer suffixes | 42u, 42L, 42LL, 42ULL | unsigned / long / long long |
| Floating point | 3.14, 2.5e3, 1.0f | double (f के साथ float) |
| Character | 'A', '7', '\n', '\t' | char |
| String | "Hello", "C:\\temp" | const char का array |
| Boolean | true, false | bool |
| Pointer | nullptr | std::nullptr_t |
C++14 में पढ़ने में आसानी के लिए digit separators भी हैं: 1'000'000 और 1000000 एक ही हैं।
#include <iostream>
int main() {
std::cout << 0x2A << " " << 052 << " " << 0b101010 << "\n";
std::cout << 2.5e3 << " " << 'A' << " " << true << "\n";
std::cout << 1'000'000 << "\n";
return 0;
}ध्यान दें कि true का output 1 आता है; शब्द true print करने के लिए std::boolalpha लगाएँ।
Character और string literals में escape sequences
| Sequence | अर्थ |
|---|---|
\n | नई line |
\t | Tab |
\\ | Backslash |
\' और \" | Single / double quote |
\0 | Null character (C string का अंत) |
const keyword
const variable को initialise होने के बाद read-only बना देता है। Declare करते समय ही उसे value देनी होती है।
#include <iostream>
int main() {
const int MAX_MARKS = 100;
const double PASS_PERCENT = 33.0;
int obtained = 72;
double percent = obtained * 100.0 / MAX_MARKS;
std::cout << "Percent: " << percent << "\n";
std::cout << (percent >= PASS_PERCENT ? "Pass" : "Fail") << "\n";
// MAX_MARKS = 90; // error: read-only variable में assignment
return 0;
}const references और pointers (const std::string& name) तथा member functions (void show() const;) के साथ भी आता है, जो आगे के lessons में मिलेंगे।
constexpr: compile time पर तय constants
constexpr (C++11) वादा करता है कि value compiler ही निकाल सकता है। इसे उन values के लिए इस्तेमाल करें जो program चलने से पहले तय होनी चाहिए, जैसे array का size।
constexpr int SUBJECTS = 5;
int marks[SUBJECTS]; // OK: size compile time पर पता है
constexpr int square(int x) { return x * x; }
constexpr int AREA = square(4); // compiler ने निकाला: 16User input से initialise हुआ const भी const है, पर वह constexpr नहीं है:
int n; std::cin >> n; const int size = n; // ठीक: बाद में बदल नहीं सकता // constexpr int s2 = n; // error: n compile time पर पता नहीं
#define macros
C का पुराना तरीका preprocessor macro है:
#define PI 3.14159
Preprocessor compilation से पहले हर PI को 3.14159 से बदल देता है। इसका न type होता है, न scope, और गलतियाँ पकड़ना मुश्किल है। C++ में constexpr double PI = 3.14159; बेहतर है। Macros कब अब भी काम के हैं, यह देखें preprocessor directives में।
enum constants
जब कोई variable कुछ तय नाम वाली values में से ही एक ले सकता हो, तो enumeration लें:
enum class Grade { A, B, C, D, F };
Grade g = Grade::B;देखें C++ में enums।
const vs constexpr vs #define
| Feature | #define | const | constexpr |
|---|---|---|---|
| Type होता है | नहीं | हाँ | हाँ |
| Scope मानता है | नहीं | हाँ | हाँ |
| Compile time पर value तय | Text replace होता है | ज़रूरी नहीं | हाँ, हमेशा |
| Array size बन सकता है | हाँ | सिर्फ constant expression से initialise हो तो | हाँ |
| Debugger में दिखता है | नहीं | हाँ | हाँ |
आम गलतियाँ
const int x;बिना value के लिखना — const को initialise करना ज़रूरी है।- String के लिए
'Hello'(single quotes) लिखना; single quotes एक character के लिए हैं। - भूल जाना कि
07और010octal हैं:010का मान 8 है, 10 नहीं। - Integer division की गड़बड़:
72 / 100 * 100का मान0है;72 * 100.0 / 100लिखें। - Typed constants के लिए
#defineलेना और expressions में अजीब नतीजे पाना, जैसे#define TWICE 1+1सेTWICE*3का मान 4 आता है।
एक program लिखें जिसमें constexpr int DAYS_IN_WEEK = 7, const double GST = 0.18 और एक enum class Day हो। किसी item का price पढ़कर GST सहित print करें, और constant से एक हफ्ते के घंटे print करें।
सारांश
- Literals code में लिखी fixed values हैं; constants नाम वाली read-only values हैं।
- Literals के प्रकार: integer, floating, character, string, boolean और
nullptr। const= initialise होने के बाद read-only;constexpr= compile time पर तय।#defineकी जगहconst/constexprलें।- तय नाम वाली values के समूह के लिए
enum classलें।
संबंधित: variables और data types, type casting, enums।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में constant और literal में क्या अंतर है?
100, 3.14, 'A' या "Aman"। Constant एक नाम वाली value है जो set होने के बाद बदल नहीं सकती, जैसे const int MAX = 100;। Constant को अक्सर value किसी literal से ही मिलती है।C++ में const और constexpr में क्या अंतर है?
const का मतलब है initialise होने के बाद value नहीं बदलेगी, पर वह value run time पर भी तय हो सकती है। constexpr का मतलब है value compile time पर ही निकलनी चाहिए, इसलिए इसे वहाँ इस्तेमाल कर सकते हैं जहाँ compiler को fixed number चाहिए, जैसे array size।C++ में #define इस्तेमाल करें या const?
const या constexpr बेहतर हैं। उनका type होता है, वे scope के नियम मानते हैं और debugger में दिखते हैं। #define preprocessor का सादा text replacement है, इसलिए उसमें type checking नहीं होती और अजीब errors आ सकती हैं।C++ में literals कितने प्रकार के होते हैं?
42, 0x2A, 052, 0b101010), floating-point literals (3.14, 2.5e3), character literals ('A', '\n'), string literals ("Hello"), boolean literals (true, false) और pointer literal nullptr होते हैं।