Updated April 4, 2023
Introduction to C++ variable types
The following article provides an outline of C++ variable types. C++ variables act as a container for storing values of required data, and then these variables with storage can be manipulated easily. Each variable has a specific data type that determines the entire layout and size for the memory of the variable with some values and a set of operations to be stored within it. The name of the variable varies accordingly, and they can be defined and manipulated accordingly. There are various types of variables in C++ like bool, int, char, double, float, wchar_t, and void for processing.
Syntax of C++ variable types
There is a specific syntax flow for defining variables in C++, which is represented as follows :
Type_variable = value;
Where the creation of a variable must require some value to specify the type and assign that value respectively.
In the above representation, Type signifies one of the C++ types that can be int, double, char, etc., followed by the variable name then = signifies that the memory is being created and the variable is ready for taking the input as a value within the Type_variable.
Certain working conditions are prevailing in the C++ variables like these variables first need to be initialized, defined, and then declared.
- Variable is basically a name given to any memory location that will be considered a memory allocator and a basic unit for storage of any program or value.
- Operations performed on the variable affects the memory location instead of only the variable itself.
- It is mandated to declare all the variables before they are called or used at the time of execution.
- A variable name can consist of alphabets in both upper and lower case, numbers, and the underscore with some character but should never start with an integer number.
- Sometimes there is confusion between variable declaration and variable definition, whereas the mentioned variable declaration needs to be done initially before it gets used by the other variable for use at the time of execution.
- On the other hand, a variable definition is responsible for assigning any memory location and a value. Most often, variable declaration and variable definition go hand in hand simultaneously.
- There are three types of variables based on the scope of the variables in C++, which are: Local variables, Instance variables, and static variables.
- The local variable supports all the data types because the scope is limited to the local variable. It does not check for the other method that is present outside the local scope of the variable.
- Accessibility of such type of local variables is only within the block, not outside the method or block.
- Instance variables are the type of variables in C++ that are non-static variables and are declared outside any class declared in a class outside the method or any other constructor or block. These types of variables cannot access any local variables, as mentioned before.
- Static variables are the types of variables in C++, which are also known as class variables, and these variables behave somewhat like instance variables with a mere difference that static variables are declared with the keyword static within a class and outside any method or constructor.
- Any static variable will have only one copy of the static variable as per requirement irrespective of the number of objects being created. This kind of variables get created at the time of program execution and gets destroyed once the execution is finished.
- If these kinds of variables are declared without respective keywords, they will throw a compilation error at the time of execution.
Types of C++ variables
Below mention are the different types of c++ variables:
1. int
Int variable in C++ has the natural size for integer memory allocation within the machine itself.
Example:
This program demonstrates the integer type variable to be used as per requirement, which will hold good only for integer variables as shown in the output.
#include <iostream>
using namespace std;
int main()
{
int int_var = 20;
cout << int_var;
return 0;
}
Output:
2. String
A string type variable is used to hold any variable in the form of text or mix of characters and text type of value being given as input.
Example:
This program demonstrates a variable declaration which will hold the value in the form of string only, as shown in the output.
#include <iostream>
using namespace std;
int main()
{
string str_txt = "Welcome_Everyone.";
cout << str_txt;
return 0;
}
Output:
3. double
This variable is responsible for holding a double-precision floating-point value.
Example:
This program demonstrates the variable holding double floating-point type value, i.e. it can hold floating-point values with decimals as shown in the output.
#include <iostream>
using namespace std;
int main()
{
double m_float_nm = 6.20;
cout << m_float_nm;
return 0;
}
Output:
4. char
This variable is responsible for holding values related to characters in the form of a single octet one byte that is also an integer type.
Example:
This program demonstrates the variable holding any character type of value within the variable as shown in the output.
#include <iostream>
using namespace std;
int main()
{
char m_char = 'P';
cout << m_char;
return 0;
}
Output:
5. bool
This type of variable is responsible for holding values with data type in boolean format. It represents value 0 for false and gives 1 for any value which gets satisfied as true.
Example:
This program demonstrates the boolean value with false or true value as shown in the output.
#include <iostream>
using namespace std;
int main()
{
bool m_bool = false;
cout << m_bool;
return 0;
}
Output:
Conclusion
C++ variables and types of variables in C++ play a crucial role in implementing and executing any program. Since it is a part of any program set, the concepts and rules must be followed religiously; otherwise, it will be difficult to get the actual output as per requirement.
Recommended Articles
This is a guide to C++ variable types. Here we discuss the various types of C++ variables along with the examples and outputs. You may also have a look at the following articles to learn more –