www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C++ Tutorial - Lesson 2: Variables

by John Kopp

Support this site at no cost to you

Welcome to the second lesson in EasyCPlusPlus.com's C++ tutorial. This lesson will teach you how to declare and use variables in C++. A variable, or object in C++ lingo, is used to hold data within your program. A variable represents a location in your computer's memory. You can put data into this location and retrieve data out of this location. Every variable has two parts, a name and a data type.

Variable Names
Valid names can consist of letters, numbers and the underscore, but may not start with a number. A variable name may not be a C++ keyword such as if, for, else, or while. Variable names are case sensitive. So, Age, AGE, aGE and AgE could be names for different variables, although this is not recommended since it would probably cause confusion and errors in your programs. Let's look at some variable declarations to better understand these rules. Note that int, float and double are built in C++ data types as explained latter in this lesson.

List of C++ Keywords

Which of the following are valid variable names?

int idnumber;
int transaction_number;
int __my_phone_number__;
float 4myfriend;
float its4me;
double VeRyStRaNgE;
float while;
float myCash;
int CaseNo;
int CASENO;
int caseno;


ANSWERS

Data Types
C++ also allows for user defined data types. These include classes and will be discussed in the more advanced tutorials. C++ provides built in data types for boolean, character, float and integer data. Boolean variables are declared with the keyword bool and contain one of two values, true or false.

Examples:

bool myStatus = true;
bool yourStatus = false;


As an aside, in C++ you may assign a value to a variable when you declare it.

Integer variables are used to store whole numbers. There are several keywords used to declare integer variables, including int, short, long, unsigned short, unsigned long. The difference deals with the number of bytes used to store the variable in memory, long vs. short, or whether negative and positive numbers may be stored, signed vs. unsigned. These differences will be explained in more advanced tutorials. For now, use int to declare integer variables. On most systems, int is synonymous with signed long.

Examples:
int count;
int number_of_students = 30;


Float variables are used to store floating point numbers. Floating point numbers may contain both a whole and fractional part, for example, 52.7 or 3.33333333. There are several keywords used to declare floating point numbers in C++ including float, double and long double. The difference here is the number of bytes used to store the variable in memory. Double allows larger values than float. Long double allows even larger values. These differences will be explained in more advanced tutorials. For now, use float to declare floating point variables.

Examples:
float owned = 0.0;
float owed = 1234567.89;


Character variables are used character variables. The use of characters and strings will be covered in a latter tutorial. Character variables are declared with the keyword char.

Examples:
char firstInitial = 'J';
char secondInitial = 'K';

LVALUES/RVALUES
C++ has the notion of lvalues and rvalues associated with variables and constants. The rvalue is the data value of the variable, that is, what information it contains. The "r" in rvalue can be thought of as "read" value. A variable also has an associated lvalue. The "l" in lvalue can be though of as location, meaning that a variable has a location that data or information can be put into. This is contrasted with a constant. A constant has some data value, that is an rvalue. But, it cannot be written to. It does not have an lvalue.

Another view of these terms is that objects with an rvalue, namely a variable or a constant can appear on the right hand side of a statement. They have some data value that can be manipulated. Only objects with an lvalue, such as variable, can appear on the left hand side of a statement. An object must be addressable to store a value.

Here are two examples.

int x;

x = 5;  // This is fine, 5 is an rvalue, x can be an lvalue.
5 = x;  // This is illegal. A literal constant such as 5 is not
         // addressable. It cannot be a lvalue.

Previous Page       Next Page