- What Is a Variable?
- Defining a Variable
- Creating More Than One Variable at a Time
- Assigning Values to Your Variables
- typedef
- When to Use short and When to Use long
- Constants
- Enumerated Constants
- Q&A
- Workshop
Creating More Than One Variable at a Time
You can create more than one variable of the same type in one statement by writing the type and then the variable names, separated by commas. For example:
unsigned int myAge, myWeight; // two unsigned int variables long area, width, length; // three longs
As you can see, myAge and myWeight are each declared as unsigned integer variables. The second line declares three individual long variables named area, width, and length. The type (long) is assigned to all the variables, so you cannot mix types in one definition statement.
Good Commenting Practice
Putting meaningful comments on the same line as your variable declarations is a good practice. It reminds you and tells other programmers what you intended to do with that variable even if the name does not convey that information.