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 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
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 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
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 <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
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 <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
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 <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
- Writing
int a, b = 5;and thinking both are 5, when onlybis initialized andais uninitialized. - Writing
int a = b = 0;whenbwas never declared, which fails to compile. - Trying to declare different types in one statement like
int a, double b;, which is not allowed.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: