C Multiple Variables
In this page:
data_type name1, name2, name3;
data_type name1 = value1, name2 = value2, name3 = value3;
Declaring Multiple Variables of the Same Type
Multiple variables of the same type can be declared in a single statement by separating their names with commas after one type keyword, avoiding the repetition of writing the type multiple times.
Example: 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
Declaring and Initializing Together
Each variable in a comma-separated declaration can also be given its own initial value, combining declaration and initialization for several variables of the same type in one line.
Example: 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
Multiple Variables of Different Types
A single declaration statement can only introduce variables of one specific type, so variables of different types, like an int and a float, must always be declared in separate statements.
Example: 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
Multiple Assignment in One Statement
C allows chained assignment, such as x = y = z = 10, which evaluates right to left, assigning the same value to multiple variables in a single expression statement.
Example: 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
While declaring many variables on one line saves space, grouping only closely related variables together and keeping unrelated ones on separate lines generally keeps code easier to read and maintain.
Example: 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
- Writing
int a, b = 5;and assuming both are 5, when onlybis initialized. - Writing
int a = b = 0;wherebis not declared, which does not compile. - Declaring different types in one statement like
int a, float b;, which is not 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