C++ के Constants
In this page:
const data_type CONSTANT_NAME = value;
constexpr data_type CONSTANT_NAME = compile_time_value;
const Keyword
const keyword compiler को बताता है कि एक variable की value initialize होने के बाद कभी नहीं बदलनी चाहिए, और बाद में इसे assign करने की कोई भी कोशिश एक runtime bug की बजाय एक compile-time error की तरह पकड़ी जाती है।
यह tax rate या एक array size जैसी values के लिए useful है जो fixed होनी चाहिए और code में कहीं और accidental modification से protected।
उदाहरण: The const Keyword
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare taxRate and set it to 0.08
const double taxRate = 0.08;
// Print to the console with cout
std::cout << "Tax rate: " << taxRate << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant Expressions (constexpr)
constexpr value को पूरी तरह compile time पर computable होना ज़रूरी बनाकर const से एक कदम आगे जाता है, जो compiler को program चलते समय इसे compute करने की बजाय literal result को सीधे generated machine code में substitute करने देता है।
यह tight loops या array sizes में इस्तेमाल होने वाली values के लिए performance measurably improve कर सकता है।
उदाहरण: Constant Expressions (constexpr)
#include <iostream>
constexpr int arraySize = 10; // computed entirely at compile time
int main() {
int values[arraySize];
std::cout << "Array size: " << arraySize << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Literals
एक literal एक value है जो आपकी source code में exactly वैसे लिखी जाती है जैसे इस्तेमाल होगी — 42, 3.14, A, और "hello" respectively int, double, char, और string type के सब literals हैं।
Literals named constants से अलग हैं: एक literal का अपना कोई नाम नहीं होता, जबकि एक constant एक literal को एक readable, reusable label देता है।
उदाहरण: Literals
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << 42 << " " << 3.14 << " " << 'A' << " " << "hello" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
#define से Constants Define करना
#define एक preprocessor directive है जो compilation शुरू होने से भी पहले एक pure text substitution perform करता है, defined name के हर occurrence को पूरी file में इसकी value से replace करते हुए।
क्योंकि यह type system के जरिए की बजाय text level पर होता है, modern C++ आमतौर पर const या constexpr पसंद करता है, जो type-checked हैं और debug करना आसान है।
उदाहरण: Defining Constants with #define
// Include std::cout and std::cin
#include <iostream>
// Define the macro MAX_USERS (replaced by the preprocessor)
#define MAX_USERS 100
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "Max users: " << MAX_USERS << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant Best Practices
Constant names को ALL_CAPS में लिखना, जैसे MAX_USERS या PI, उन्हें code में जहाँ भी appear हों तुरंत fixed values की तरह recognizable बनाता है, उन्हें एक नज़र में ordinary variables से अलग करते हुए।
Related constants को एक file के top के पास साथ group करना भी एक जगह config-जैसी values ढूंढना और update करना आसान बनाता है।
उदाहरण: Constant Best Practices
// Include std::cout and std::cin
#include <iostream>
// Declare PI and set it to 3.14159
const double PI = 3.14159;
// Declare MAX_USERS and set it to 100
const int MAX_USERS = 100;
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << PI << " " << MAX_USERS << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- बाद में एक
constvariable बदलने की कोशिश करना, जैसेPI = 3;, जो एक compile error है। - बिना एक initial value के
const int x;declare करना, जो एक error है क्योंकि एक constant को initialize होना ज़रूरी है। - एक
#defineline को#define MAX 100;जैसे semicolon से खत्म करना, जो फिर हर इस्तेमाल में paste हो जाता है और expressions तोड़ देता है।
- C++ एक compiled language है, और एक पहला program इसका basic syntax, statements, comments, keywords, और identifiers दिखाता है।
- numeric types,
bool, औरcharजैसे data types describe करते हैं कि variables कौन सी values रख सकते हैं। - Variables एक बार में एक या कई एक साथ declare किए जा सकते हैं, और constants ऐसी values रखते हैं जो नहीं बदलनी चाहिए।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: