C++ Multiple Variables

In C++, you can declare multiple variables of the same type in a single statement. This helps make code cleaner and more efficient.

Tutorials dojo strip

Declaring Multiple Variables

Instead of declaring variables separately, you can group them together using commas:

Example

C++

Explanation:

  • The variables speedA, speedB, and speedC are all declared as integers (int) in a single line.
  • Their values (120, 150, 180) are assigned immediately.
  • The sum of these values is printed using cout.

Assigning One Value to Multiple Variables

C++ also allows multiple variables to be assigned the same value in one step:

Example

C++

Explanation:

  • The variables bike1, bike2, and bike3 are all declared without immediate values.
  • They are later assigned the same value (220) in a single line.
  • The program outputs the sum of all three values.
Tutorials dojo strip