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

C++ में Multiple Variables

C++ same type के कई variables को एक single comma-separated statement में declare, और optionally initialize, करने देता है, और कई variables में chained assignment भी support करता है।
Syntax
cpp
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

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

साथ में Declare और Initialize करना

एक comma-separated declaration में हर variable को उसकी अपनी initial value भी दी जा सकती है, एक line में same type के कई variables के लिए declaration और initialization combine करते हुए।

उदाहरण: Declaring and Initializing Together

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

अलग Types के कई Variables

एक single declaration statement सिर्फ एक specific type के variables introduce कर सकता है, इसलिए एक int और एक float जैसे अलग types के variables को हमेशा अलग statements में declare करना होता है।

उदाहरण: Multiple Variables of Different Types

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

एक 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

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

Readability Considerations

एक line में कई variables declare करना space बचाता है, लेकिन सिर्फ closely related variables को साथ group करना और unrelated वालों को अलग lines पर रखना आमतौर पर code को पढ़ना और maintain करना आसान रखता है।

उदाहरण: Readability Considerations

cpp
#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;
}
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. int a, b = 5; लिखना और सोचना कि दोनों 5 हैं, जब सिर्फ b initialize हुआ है और a uninitialized है।
  2. int a = b = 0; लिखना जब b कभी declared नहीं हुआ, जो compile होने में fail होता है।
  3. एक statement में अलग types declare करने की कोशिश करना जैसे int a, double b;, जो allowed नहीं है।

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.