← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 15 of 15

C++ के Constants

एक constant एक value है जिसे आप कभी न बदलने का वादा करते हैं, जैसे एक हफ्ते के दिनों की संख्या। C++ आपको इसे const या constexpr से lock करने देता है।
Syntax
cpp
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

cpp
// 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;
}

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)

cpp
#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;
}

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

cpp
// 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;
}

#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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बाद में एक const variable बदलने की कोशिश करना, जैसे PI = 3;, जो एक compile error है।
  2. बिना एक initial value के const int x; declare करना, जो एक error है क्योंकि एक constant को initialize होना ज़रूरी है।
  3. एक #define line को #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 रखते हैं जो नहीं बदलनी चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.