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 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;
}
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 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;
}
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 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;
}
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 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;
}
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 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;
}
Login to try C/C++/Java/PHP code in the editor
int a, b = 5;लिखना और मान लेना कि दोनों 5 हैं, जब सिर्फbinitialize हुआ है।int a = b = 0;लिखना जहाँbdeclared नहीं है, जो compile नहीं होता।- एक statement में अलग types declare करना जैसे
int a, float b;, जो allowed नहीं है।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers