Elijah Hazelton
PRO
last year
Elijahcountry asked

I noticed that age and id were both declared as integers on the same line. Is it only useful to declare variables this way if they’re the same type?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Elijah,

Yes, exactly. In C++, when you declare multiple variables on the same line, they all have to be the same type. So if you're declaring int age, id;, both variables are integers.

This is mostly for convenience and cleaner code when you're working with variables of the same type. But if the variables are different types — like an int for age and a string for name — then you'd declare them separately:

int age = 24;
string name = "John";

So grouping variables on one line only works when their data type is the same.

If you have more questions, I’m here to help.

C++
This question was asked as part of the Learn C++ Basics course.