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

C++ के Variables

एक variable एक labeled box की तरह है जहाँ आप information रखते हैं। आप बताते हैं कि इसमें किस तरह की चीज़ जाएगी, इसे एक नाम देते हैं, और बाद में अंदर क्या है यह बदल सकते हैं।
Syntax
cpp
data_type variable_name;          // declaration
variable_name = value;            // assignment

data_type variable_name = value;  // declaration with initialization

Variable Declaration

एक variable declare करना इसके data type के लिए sized memory का एक named slot reserve करता है — int age; लिखना compiler को 4 bytes अलग रखने और उस space को इसके बाकी scope के लिए age की तरह refer करने को कहता है।

आपको इस्तेमाल से पहले एक variable का type declare करना ज़रूरी है, क्योंकि C++ को जानना होता है कि कितने bytes allocate करने हैं और उन्हें कैसे interpret करना है।

उदाहरण: Variable Declaration

cpp
#include <iostream>

int main() {
	int age; // reserves 4 bytes named 'age'
	age = 30;
	std::cout << age << std::endl;
	return 0;
}

Variable Initialization

Initialization एक variable की starting value उसी statement में set करता है जो इसे declare करता है, जैसे int age = 25;, जो एक uninitialized variable पढ़ने की bug-prone situation से बचता है जो unpredictable leftover memory रखता है।

C++ में best practice है कि जब भी कोई sensible starting value exist करे variable को declaration के point पर हमेशा initialize करें।

उदाहरण: Variable Initialization

cpp
#include <iostream>

int main() {
	int age = 25; // declared and initialized in one statement
	std::cout << age << std::endl;
	return 0;
}

कई Variables

आप उनके नामों को commas से अलग करके एक line में same type के कई variables declare कर सकते हैं, जैसे int x, y, z;, जो related declarations को visually grouped रखता है।

हर नाम को अभी भी अपनी independent storage मिलती है — वे बस brevity के लिए एक type declaration share करते हैं।

उदाहरण: Multiple Variables

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int x, y, z;
	x = 1; y = 2; z = 3;
	// Print to the console with cout
	std::cout << x << " " << y << " " << z << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dynamic Initialization

Dynamic initialization का मतलब है एक variable की starting value एक fixed literal होने की बजाय runtime पर एक expression से compute होती है, उदाहरण के लिए int area = width * height; जहाँ width और height user ने enter किए थे।

यही वह चीज़ है जो variables को constants से आगे genuinely useful बनाती है — उनकी values आपका program चलते हुए compute करने वाले दूसरे data पर depend कर सकती हैं।

उदाहरण: Dynamic Initialization

cpp
#include <iostream>

int main() {
	int width = 10;
	int height = 5;
	int area = width * height; // computed at runtime, not a fixed literal
	std::cout << "Area: " << area << std::endl;
	return 0;
}

Variable Scope

एक variable की scope decide करती है कि आपके code में इसे कहाँ access किया जा सकता है: local variables सिर्फ उस function या block के अंदर exist करते हैं जहाँ वे declared हैं और वह block खत्म होते ही गायब हो जाते हैं, जबकि किसी भी function के बाहर declared global variables file में हर जगह visible होते हैं।

Global variables से ज़्यादा local variables पसंद करना functions को self-contained और isolation में test करना आसान रखता है।

उदाहरण: Variable Scope

cpp
#include <iostream>

int globalCount = 100; // visible everywhere in the file

void localExample() {
	int localCount = 5; // only exists inside this function
	std::cout << localCount << std::endl;
}

int main() {
	localExample();
	std::cout << globalCount << std::endl;
	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. एक ऐसा variable इस्तेमाल करना जो declared तो था लेकिन कभी initialize नहीं हुआ, जैसे int x; cout << x;, जिसकी एक indeterminate value है।
  2. same scope में same variable नाम दोबारा declare करना जैसे int a = 1; int a = 2;, जो एक compile error है।
  3. एक variable को उस block के बाहर इस्तेमाल करना जहाँ यह declared था, जब यह closing brace पर out of scope हो जाता है।

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.