Updated March 17, 2023
Introduction to Variables in C++
Variables in C++ acts as a memory location, it is nothing but the name of the container or element that stores the data or values that are being used in the program later for execution. It can be defined using the combination of letters digits, or special symbols like underscore(_), defined by using the data types like char, int, float, double. Variables can be anything except the reserved keyword, the first letter of the variables must start with the letter only.
Variables are the most important part of any programming language. Any programming language is incomplete without a variable. We can also say that without variables, the program cannot run. Like any other programming language, the C++ language also needs variables to run their program. Variables are not used to run the program, instead, they are used to store the value or string. Without storing value, the program cannot run. Hence, variables are known for the backbone of the programming language. In C++ any word except the keywords is used as a variable. To define variables we need to specify the type for the variable. Type can be anything int, double, char, float, long int, short int, etc. int is used to store integer value i.e. 5, 19, 519, 1000. Char is used to storing the character or string i.e. a, educate. Float is used to store the float values like 2.3, 3.679, 9.45. Long int is used to store long integer values. In this article, we are going to discuss how to initialize and declare the variables in the C++ language. And the types of variables.
Rules and Regulations for Defining Variables in C++ Language
- Variables can be a mixture of digits, special characters like and percent (&), underscore (_) or string.
- Upper and lower cases are treated as different variables as C++ is case sensitive language. Educba and eduCBA will be treated as two different variables.
- C++ variables must be started with the character. It will not consider the number as a first character. 6educba is not a valid variable because it starts with the number where educba6 can be a valid variable as it started with the character.
- variables in C++ language should not be a keyword. for, this, if, else, while, do, char, this, etc are the keywords that are used for the specific purpose. These keywords cannot be used as a variable in C++.
- Blank spaces are not allowed for the variables. Edu cba is not valid as there is space between edu and cba where educba is valid variable or edu_cba is also a valid variable as underscore sign is used to join the variable.
How do Variables Work in C++ Language?
- Declaration of variables informs compiler the types of data variables will be used in the program.
- Declaration of variables names informs compiler the name of the variables that are used to store the value in the program.
- While declaring variables I tell the compiler the storage that variables need. The compiler does not have to worry about the storage until it is declared.
How to Declare Variables in C++ Language?
Variables can be declared first before starting with the programs. The syntax for declaration of a variable is as follows
data_type variable_name;
where
data_type: Defines types of data for storing value. Data types can be int, char, float, double, short int, etc.
variable_name: Defines the name of the variables. It can be anything except the keyword.
For example,
1. int cab;
2. float 6.9, 7.3
For example 1, int is a data type and cab is a variable name. In the second example, we have declared two variables where the float is a data type and 6.9 and 7.3 are variables.
Once the variables are declared, the storage for those variables has been allocated by the compiler as it will be used for the program.
Program to Illustrate the Declaration of Variables in C++ Language
#include<iostream>
using namespace std;
int main()
{
int x, y, z;
x = 10;
y = 3;
z = x + y;
cout << "Sum of two numbers is: " << z;
return 0;
}
How to Initialize Variables in C++ Language?
In C++, variables can be initialized by assigning the values at the time of declaration. The syntax for initialization of variables in C++ language is –
data_type variable_name = value;
For example,
- int x = 10;
- char b = ‘eduCBA’
In example 1, we initialized variable x with value 10. In example 2, we have initialized b as a character with eduCBA value.
Program to Illustrate Initialization of Variables in C++ Language
#include<iostream>
using namespace std;
int main()
{
int x = 5, y = 15;
int z = x + y;
cout << "Sum of two numbers is: "<< z;
return 0;
}
Types of Variables in C++ Language
There are 5 types of variables in C++ language which are as follows:
1. Local Variables
Local variables are declared inside the function. Local variables must be declared before they have used in the program. Functions that are declared inside the function can change the value of variables. Functions outside cannot change the value of local variables.
Here is an example
int main()
{
int x = 2; //local variable
}
2. Global Variables
Global variables are declared outside the functions. Any functions i.e. both local function and global function can change the value of global variables.
Example is given as follows,
int y = 10; //global variable
int main()
{
int x = 5; //local variable
}
3. Static Variables
These variables are declared with the word static.
Example is given as follows,
int main()
{
int x = 5; //local variable
static y = 2; //static variable
}
4. Automatic Variables
Automatic variables are declared with the auto keyword. All the variables that are declared inside the functions are default considered as an automatic variable.
Example is given as follows,
int main()
{
int x = 20; //local variable (Automatic variable)
auto y = 12; //automatic variable
}
5. External Variables
By using the extern keyword, external variables are declared.
extern z = 4; //external variable
Conclusion
In this article, we have seen the importance of variables in C++ language and how to work with variables with the help of examples. Also, we have seen five different types of variables in the C++ language with examples. I hope you will find this article helpful.
Recommended Articles
This is a guide to Variables in C++. Here we discuss Introduction, how to use Variables in C++ along with Examples. You can also go through our other suggested articles –