C++ Variable Types
A variable is, in essence, just a name for a storage area that the program can manipulate.
In C++, there are many variable types for storing different kinds of data.
Every variable in C++ has a specified type; the type determines the size and layout of the variable's storage, the range of values that can be stored in memory, and the operations that can be applied to the variable.
A variable name can consist of letters, digits, and underscore characters. It must begin with a letter or an underscore.
Uppercase and lowercase letters are different because C++ is case-sensitive.
Based on the basic types explained in the previous chapter, the following basic variable types will be explained in the next chapter:
| Type | Description |
|---|---|
| bool | bool type, stores the value true or false, occupying 1 byte. |
| char | char type, used to store ASCII characters, usually occupying 1 byte. |
| int | int type, usually used to store ordinary integers, usually occupying 4 bytes. |
| float |
float type, used to store single-precision floating-point numbers. Single precision has the format: 1 sign bit, 8 exponent bits, 23 fraction bits, usually occupying 4 bytes.
|
| double |
double type, used to store double-precision floating-point numbers. Double precision has 1 sign bit, 11 exponent bits, 52 fraction bits, usually occupying 8 bytes.
|
| void | Represents the absence of a type. |
| wchar_t | wchar_t type, used to store a wider range of characters, usually occupying 2 or 4 bytes. |
C++ also allows defining various other types of variables, such asenum, pointer, array, reference, data structure, classand so on, which will be explained in later chapters.
-
Integer Types:
int: used to represent integers, usually occupying 4 bytes.short: used to represent short integers, usually occupying 2 bytes.long: used to represent long integers, usually occupying 4 bytes.long long: used to represent longer integers, usually occupying 8 bytes.-
Floating-Point Types:
float: used to represent single-precision floating-point numbers, usually occupying 4 bytes.double: used to represent double-precision floating-point numbers, usually occupying 8 bytes.long double: used to represent higher-precision floating-point numbers; the number of bytes occupied may vary by implementation.-
Character Types:
char: used to represent characters, usually occupying 1 byte.wchar_t: used to represent wide characters, usually occupying 2 or 4 bytes.char16_t: used to represent 16-bit Unicode characters, occupying 2 bytes.char32_t: used to represent 32-bit Unicode characters, occupying 4 bytes.-
Boolean Type:
bool: used to represent boolean values, can only taketrueorfalse.-
Enumeration Types:
enum: used to define a set of named integer constants.-
Pointer Types:
type*: used to represent a pointer to an object of typetype.-
Array Types:
type[]ortype[size]: used to represent an array composed of elements of the same type.-
Structure Types:
struct: used to define a structure containing members of different types.-
Class Types:
class: used to define a custom type with attributes and methods.-
Union Types:
union: used to define a special data type that can store different data types at the same memory location.In C++, the size of a type (i.e., the number of bytes occupied) depends on the compiler and the computer architecture. However, the C++ standard specifies the minimum ranges for different integer types rather than specific byte counts, so that code runs correctly on different systems.
Note that the ranges above are only the minimum requirements of the C++ standard; in practice, these types may occupy more bytes on many systems—for example, on many modern computers int usually occupies 4 bytes while long may occupy 8 bytes.
Below we will explain how to define, declare, and use variables of various types.
Variable Definition in C++
A variable definition tells the compiler where and how to create the storage for the variable.

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 and can consist of one or more identifier names separated by commas. Several valid declarations are listed below:
int
i
,
j
,
k
;
char
c
,
ch
;
float
f
,
salary
;
double
d
;
line int i, j, k; declares and defines the variables i, j, and k, instructing the compiler to create variables named i, j, k of type int.
A variable may be initialized (assigned 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
;
A few examples are listed below:
extern
int
d
=
3
,
f
=
5
;
// declaration of d and f
int
d
=
3
,
f
=
5
;
// define and initialize d and f
byte
z
=
22
;
// define and initialize z
char
x
= '
x
';
// the value of variable x is 'x'
Definitions without initialization: variables with static storage duration are implicitly initialized to NULL (all bytes are 0); the initial values of all other variables are undefined.
Variable Declaration in C++
A variable declaration assures the compiler that a variable exists with the given type and name, so the compiler can continue further compilation without knowing the variable's full details. A variable declaration is meaningful only at compile time; at program linking the compiler needs the actual variable definition.
Variable declarations are very useful when you use multiple files and define a variable in only one of them (the file that defines the variable is available at program linking). You can use extern the keyword to declare a variable anywhere. Although you can declare a variable multiple times in a C++ program, the variable can be defined only once in a file, function, or block.
Example
Try the example below, where the variables are declared at the top but defined and initialized inside the main function:
Example
#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 code above is compiled and executed, it produces the following result:
30
23.3333
Likewise, when declaring a function, you provide a function name, while the actual definition of the function can be placed 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 kinds of expressions in C++:
- **Lvalue:** An expression that refers to a memory location is called an lvalue expression. An lvalue may appear on either the left or the right side of an assignment operator.
- **Rvalue:** The term rvalue refers to a data value stored at some address in memory. An rvalue is an expression to which a value cannot be assigned—that is, an rvalue may appear on the right side of an assignment operator 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 to and cannot appear on the left side of an assignment. The following is a valid statement:
int g = 20;
But the following is not a valid statement and will produce a compile-time error:
10 = 20;

