C++ Variable Types
A variable is essentially just a name for a storage area that a program can manipulate. Each variable in C++ has a specified type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the operations that can be applied to the variable.
The name of a variable can consist of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Uppercase and lowercase letters are distinct because C++ is case-sensitive.
Based on the basic types explained in the previous chapter, the following are the fundamental variable types, which will be discussed in the next chapter:
Type | Description |
---|---|
bool | Stores the values true or false. |
char | Typically a single character (eight bits). This is an integer type. |
int | The most natural size of an integer for the machine. |
float | Single-precision floating-point value. The format is 1 bit for the sign, 8 bits for the exponent, and 23 bits for the mantissa. |
double | Double-precision floating-point value. The format is 1 bit for the sign, 11 bits for the exponent, and 52 bits for the mantissa. |
void | Indicates the absence of a type. |
wchar_t | Wide character type. |
C++ also allows the definition of various other types of variables, such as enumerations, pointers, arrays, references, data structures, classes, etc., which will be discussed in subsequent chapters.
Next, we will explain how to define, declare, and use variables of various types.
Variable Definition in C++
A variable definition tells the compiler where to create the storage for the variable and how to create that storage.
A variable definition specifies a data type and includes a list of one or more variables of that type, as shown below:
type variable_list;
Here, type must be a valid C++ data type, which can be char, wchar_t, int, float, double, bool, or any user-defined object. **variable_list** can consist of one or more identifier names separated by commas. Here are some valid declarations:
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k, which instructs the compiler to create variables named i, j, and k of type int.
Variables can be initialized (given an initial value) at the time of declaration. An initializer consists of an equal sign followed by a constant expression, as shown below:
type variable_name = value;
Here are some examples:
extern int d = 3, f = 5; // Declaration of d and f
int d = 3, f = 5; // Definition and initialization of d and f
byte z = 22; // Definition and initialization of z
char x = 'x'; // The value of variable x is 'x'
Uninitialized variables with static storage duration are implicitly initialized to NULL (all bytes set to 0). The initial values of all other variables are undefined.
Variable Declaration in C++
A variable declaration ensures that the variable exists with the given type and name, allowing the compiler to continue compilation without needing the full details of the variable. A variable declaration is meaningful only at compile time and the actual variable declaration is needed during program linking.
Variable declarations are useful when you use multiple files and define the variable in only one of them (the file where the variable is defined must be available during program linking). You can declare a variable anywhere in your C++ program using the extern keyword. Although you can declare a variable multiple times in your C++ program, it can be defined only once in a file, function, or code block.
Example
Try the following example, where variables are declared at the top, but they are defined and initialized inside the main function:
#include <iostream>
using namespace std;
// Variable declaration
extern int a, b;
extern int c;
extern float f;
int main ()
{
// Variable definition
int a, b;
int c;
float f;
// Actual initialization
a = 10;
b = 20;
c = a + b;
cout << c << endl ;
f = 70.0/3.0;
cout << f << endl ;
return 0;
}
When the above code is compiled and executed, it produces the following result:
30
23.3333
Similarly, when declaring a function, you provide a function name, and the actual definition can be done anywhere. For example:
// Function declaration
int func();
int main()
{
// Function call
int i = func();
}
// Function definition
int func()
{
return 0;
}
Lvalues and Rvalues in C++
There are two types of expressions in C++:
- Lvalue: Expressions that refer to a memory location.
- Rvalue: Expressions that refer to data value stored at some address in memory.
Lvalue: An expression that refers to a memory location is called an lvalue expression. Lvalues can appear on either the left or the right side of an assignment.
Rvalue: The term rvalue refers to a value stored in memory at some address. Rvalues are expressions that cannot be assigned to, meaning they can appear on the right side of an assignment but not on the left.
Variables are lvalues, so they can appear on the left side of an assignment. Numeric literals are rvalues, so they cannot be assigned and cannot appear on the left side of an assignment. The following is a valid statement:
int g = 20;
However, the following is not a valid statement and will generate a compile-time error:
10 = 20;