Types of variable in C++

Types of variable in C++
Techiio-author
Written by Sagar RabidasFebruary 2, 2022
9 min read
C++
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Types of variables in C++

C++ variable types

C++ variables act as a box for storing values of the required information, after which those variables with a garage may be manipulated effortlessly. Every variable has a particular facts kind that determines the complete layout and size for the reminiscence of the variable with some values and a set of operations to be saved inside it. The call of the variable varies, therefore, and it could be defined and manipulated hence. There are numerous types of variables in c++ like bool, int, char, double, waft, 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 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 affect 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 types 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. These kinds of variables get created at the time of program execution and get 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 is the different types of c++ variables:

  • 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;
}
  • 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 that 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;
}
  • 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;
}
  • 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;
}
  • 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;
}

C
C++
TypesOfVariableInC++
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance