So lets understand what are variables, constants and data-types in c++. To understand variables and constants we can relate them with a box and a object. A box can be considered as a variable while a object can be considered as constant. So basically we store the objects(constants) inside the boxes(variables). This is the perfect analogy i can come up with. So lets understand this practically. You used to have a subject called algebra in your school right? In algebra you used to perform certain operations like :
[cpp]X = 10
Y = 5
Z = X + Y = 15[/cpp]
Here we are storing value of 1o inside variable x, value of 5 inside y and sum of x and y is passed as a value to variable z. So here X, Y, Z are the variables. While 5, 10, 15 are constants.
In C++, the types of C++ constants are divided into Primary Constants and Secondary constants. Primary constants consist of Integer Constants, Real Constants and Character Constants. While secondary constants consist of Array, Pointer, Structure, Union, etc.
Since there are different types of constants, there is a need for difference types of variables to handle different types of constants. These variable data-types are called as Data-types in C++. The following table shows the same :
As you see in the above picture, Data-types in c++ are classified into three categories, namely user-defined types, built-in types and derived types. Where user defined types are the one which are defined by us (like structure, union, class, enumeration). And derived types are the ones which are derived from existing data types (like array, pointer, reference).
The built in data-type is again classified into three categories, which are integer (int and char), void and floating type(float and double). These are the pre defined data types inside c++ programming language.
- int can only accept integer type of value.
- char can store only 1 character.
- float can store real constants which contain decimal.
- double is same as float, just the range is wider
- void means ‘null’ or nothing. Or as good as ’empty’.
Declaring and Creating Variables
Now you have a idea of what are variables, constants and data-types. So now we are ready to learn how to declare and create variables in c++ programming language.
The syntax to declare variable is given below :
[cpp]data-type variable-name ;[/cpp]
We can even write these two lines as a comma separated list as follows :
[cpp]int x, y;[/cpp]
This is the way to declare variables. Now to create variables we have to assign values to variables which are already declared before. The syntax for creating variable is :
[cpp]variable name = value ;[/cpp]
[cpp]x = 10;
y = 5;[/cpp]
Up till now we declared x and y variables. Then we created these variables in memory by assigning values to the created variables. Instead of taking many lines, we can write these lines in short by using short-hand property. The syntax to declare and create variables in one line is given below :
[cpp]datatype variable-name = value ;[/cpp]
[cpp]int x = 10 ;
int y = 5 ;[/cpp]
Now if we want to write these two in one single line, we can use comma separated list as shown below :
[cpp]int x = 10, y = 5 ;[/cpp]
Ok so now we know how to declare and create variables but there are certain rules to construct variable names. Lets see what are these rules :
- The first character in a variable name must be an alphabet or an underscore.
- No commas or blank spaces are allowed within a variable name.
- No special symbol other than an underscore ( _ ) can be used in a variable name.
Variables can be even dynamically initialized . That means we can do some operation on the right hand side and assign to left hand side. Lets see what it actually means in the examples below :
[cpp]int z = x + y ; // addition is operated on right hand side and stored to z
int a = x / y ; // similarly, division takes place here
int b = x * y ; // and multiplication takes place here..[/cpp]
Lets learn how to display value of variables in the output. The concept is that if we pass variable to console out ( cout ) by using insertion operator ( << ) then the value contained within that variable will be displayed as the output. But if we pass a variable in quotation marks ( ” ” ) to console out ( cout ) by using insertion operator ( << ) then that variable itself is displayed on the screen. Lets understand the following example :
[cpp]int x = 10;
cout << x ;
cout << "x" ;[/cpp]
Putting it all together
Now grouping together all the points we learned, lets write a simple program as an exercise for this tutorial. So the program below is to demonstrate creation of variables and displaying variables as output.
void main ()
int x = 10, y = 5;
int z = x + y ;
cout << " value of x = " << x ;
cout << " value of y = " << y ;
cout << " value of z = x+ y = " << z ;
[cpp]value of x = 10
value of y = 5
value of z = x + y = 15[/cpp]
So that’s all in this tutorial. Keep practicing and if you have any doubt you can leave a comment below. Take care.