Variables in C++ are used to store values. Each variable is stored in different locations in memory. To learn about variable types and variable declaration in C++, readers can refer to the following C++ lesson by Mytour.
Variable types and variable declaration in C++
Article Contents
1. Variables in C++
2. Variable types in C++
3. Variable declaration and initialization in C++
4. Variable Scope in C++
4.1. Global Variables in C++
4.2. Local Variables in C++
5. Some Special Variable Types in C++
Variables in C++
Variables represent memory locations that are allocated by the compiler, depending on the data types of the variables. In C++, variables can be declared in various ways, requiring different memory and functions.
Data Types in C++
In C++, variables must be declared and assigned a type. Similar to other programming languages, C++ also supports various data types. Below is a table listing the data types in C++:
Variable Types Description
Bool For variables to store boolean values (true or false)
Char For variables to store character values
int For variables with integer values
Float or double For variables with floating-point values and large values
Declaration and Initialization of Variables in C++
Variables must be declared before they are used. Typically, we declare variables when starting the program. However, in C++, we can declare variables in the middle of the program, but they must be declared before being used.
For example:
int i; // declaration without initialization
char c;
int i, j, k; // multiple declarations
Initializing a variable means assigning a value to a declared variable.
int i; // declaration
i = 10; // initialization
Additionally, we can also declare and initialize a variable at the same time:
int i=10; // initialization and declaration in one step
int i=10, j=11;
If a variable is declared but not initialized, it means it holds garbage value. Additionally, if a variable has been declared, attempting to declare it again will result in a compile-time error.
int i,j;
i=10;
j=20;
int j=i+j; //compile time error, cannot redeclare a variable in the same scope
Variable Scope in C++
All variables have a scope, where the program variables operate independently. Variables will not hold values outside the scope, and this scope is called the variable's scope. In most cases, variables declared within curly braces are called local variables.
In C++, there are 2 variable scopes:
- Global Variable.
- Local Variable.
Global Variable in C++
Global variables are variables declared once and can be used by any class or function throughout the program's lifetime. These variables must be declared outside the main() function. If only declared, they can be assigned different values at different times during the program's lifetime.
If declared and initialized outside the main() function, these variables can be assigned any values at any point in the program.
The following example illustrates global variables that are only declared and not initialized:
Local Variables in C++
Local variables are variables within curly braces. These variables are not accessible outside this scope and will result in a compile-time error.
Below is an example of local variables in C++:
Some Special Variable Types in C++
Additionally, there are other special keywords in C++ used to indicate unique characteristics of variables in the program, including:
- Final: Once initialized, its value cannot be changed.
- Static: These variables store their values across function calls.
Example:
So above Mytour just introduced you to variables, variable types, and variable declarations in C++. If you have any questions or need clarification on topics like Comments in C++, readers can leave their comments below the article, Mytour will answer your questions as soon as possible.
