C++ में Multiple Variables
In this page:
data_type name1, name2, name3;
data_type name1 = value1, name2 = value2, name3 = value3;
Same Type के कई Variables Declare करना
same type के कई variables एक type keyword के बाद उनके नामों को commas से अलग करके एक single statement में declare किए जा सकते हैं, type को कई बार लिखने की repetition से बचते हुए।
उदाहरण: Declaring Multiple Variables of the Same Type
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int x, y, z;
// Print to the console with cout
std::cout << "Declared x, y, z with one int keyword" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
साथ में Declare और Initialize करना
एक comma-separated declaration में हर variable को उसकी अपनी initial value भी दी जा सकती है, एक line में same type के कई variables के लिए declaration और initialization combine करते हुए।
उदाहरण: Declaring and Initializing Together
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int 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
अलग Types के कई Variables
एक single declaration statement सिर्फ एक specific type के variables introduce कर सकता है, इसलिए एक int और एक float जैसे अलग types के variables को हमेशा अलग statements में declare करना होता है।
उदाहरण: Multiple Variables of Different Types
#include <iostream>
int main() {
int age = 25;
float price = 9.99f; // different type needs its own statement
std::cout << age << " " << price << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Statement में कई Assignment
C++ chained assignment allow करता है, जैसे x = y = z = 10, जो right से left evaluate होता है, एक single expression statement में कई variables को same value assign करते हुए।
उदाहरण: Multiple Assignment in One Statement
#include <iostream>
int main() {
int x, y, z;
x = y = z = 10; // evaluated right to left
std::cout << x << " " << y << " " << z << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Readability Considerations
एक line में कई variables declare करना space बचाता है, लेकिन सिर्फ closely related variables को साथ group करना और unrelated वालों को अलग lines पर रखना आमतौर पर code को पढ़ना और maintain करना आसान रखता है।
उदाहरण: Readability Considerations
#include <iostream>
int main() {
int width, height; // closely related, grouped together
int score = 0; // unrelated, kept on its own line
width = 10; height = 5;
std::cout << width << " " << height << " " << score << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
int a, b = 5;लिखना और सोचना कि दोनों 5 हैं, जब सिर्फbinitialize हुआ है औरauninitialized है।int a = b = 0;लिखना जबbकभी declared नहीं हुआ, जो compile होने में fail होता है।- एक statement में अलग types declare करने की कोशिश करना जैसे
int a, double b;, जो allowed नहीं है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: