C++ के Variables
In this page:
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
#include <iostream>
int main() {
int age; // reserves 4 bytes named 'age'
age = 30;
std::cout << age << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int age = 25; // declared and initialized in one statement
std::cout << age << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Variables
आप उनके नामों को commas से अलग करके एक line में same type के कई variables declare कर सकते हैं, जैसे int x, y, z;, जो related declarations को visually grouped रखता है।
हर नाम को अभी भी अपनी independent storage मिलती है — वे बस brevity के लिए एक type declaration share करते हैं।
उदाहरण: Multiple Variables
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
- एक ऐसा variable इस्तेमाल करना जो declared तो था लेकिन कभी initialize नहीं हुआ, जैसे
int x; cout << x;, जिसकी एक indeterminate value है। - same scope में same variable नाम दोबारा declare करना जैसे
int a = 1; int a = 2;, जो एक compile error है। - एक variable को उस block के बाहर इस्तेमाल करना जहाँ यह declared था, जब यह closing brace पर out of scope हो जाता है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: