Introducing variables
A variable is a location in memory that holds a value of a specified type that can vary over the life of the variable, identified by its name. When the variable is defined with both a type and an identifier, its life begins. It can hold the same value throughout its life, or it can be modified or overwritten with a new value of that type. The variable's life ends – that is, the memory it identifies is deallocated – when the block that it was declared in ends. We'll talk more about variable lifetimes in Chapter 25, Understanding Scope.
So, a variable is a memory location with an identifier (name) associated with a type that contains a value. The following three components are essential:
- A unique identifier or name
- A type
- A value
A variable is created by declaring it; this is also called defining a variable. When we declare a variable, we specify the data type to be stored...