← Back to C Course | Chapter 1: Introduction & Basics | Lesson 19 of 21

C में Multiple Variables

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a, b, c;
	a = 1; b = 2; c = 3;
	// Print formatted text to the screen
	printf("%d %d %d", a, b, c);
	// 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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 1, b = 2, c = 3;
	// Print formatted text to the screen
	printf("%d %d %d", a, b, c);
	// 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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Declare height and set it to 5.9f
	float height = 5.9f;
	// Print formatted text to the screen
	printf("%d %.1f", age, height);
	// Return 0 to signal that the program finished successfully
	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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int x, y, z;
	x = y = z = 10;
	// Print formatted text to the screen
	printf("%d %d %d", x, y, z);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Readability Considerations

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

उदाहरण: Readability Considerations

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int width = 10, height = 20;
	// Declare price and set it to 9.99f
	float price = 9.99f;
	// Print formatted text to the screen
	printf("%d %d %.2f", width, height, price);
	// 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. int a, b = 5; लिखना और मान लेना कि दोनों 5 हैं, जब सिर्फ b initialize हुआ है।
  2. int a = b = 0; लिखना जहाँ b declared नहीं है, जो compile नहीं होता।
  3. एक statement में अलग types declare करना जैसे int a, float 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.