First of all, as variables a basic topic in C++, readers must know some facts about C++, because if anyone asks you, you’re well prepared to answer. So, C++ is a general-purpose cross-platform language that was developed by “Bjarne Stroustrup” as an extension of ‘C’ with classes. It’s used to develop(create) high-performance applications and can also be found in operating systems and GUIs (Graphical User Interface).
Variables are memory locations that we assign to store data or a particular value that can be changed when needed. It can be declared in different ways according to which it will have memory requirements.
Declaration:-
data type variable name = value to be stored;
For Example:
int v=4;
Inside Program:
#include <iostream>
using namespace std;
int main ()
{
int a, b; //Main declaration of integers
int sum;
a = 3; //initialisation of variables
b = 2;
sum = a + b;
cout << sum; // print the sum
return 0; // terminate the program:
}
Contributed By: Shiva Patra
Let’s jump to types of variables
1) int:
It is used to store integer values like 1,234,-489.
The declaration is already given in above example.
2) char:
It is used to store characters like ‘a’, ’B’, ’V’. Make sure the values are surrounded by quotes.
Declaration Example:
char myfirst = 'S';
Inside Programming:
#include <iostream>
using namespace std;
int main ()
{
char myfirst = 'S'; // Declaration and initialiation of character variable
cout << myfirst; // print the character
return 0; // terminate the program:
}
Contributed By: Shiva Patra
3) bool:
It stores the Boolean value which can be true or false.
Declaration Example:
bool statementvalue = true;
Inside Programming:
#include <iostream>
using namespace std;
int main ()
{
bool statementvalue = true; // Boolean (true or false)
cout << statementvalue; // output will be 1 as it's the true value
return 0; // terminate the program:
}
Contributed By: Shiva Patra
4)double and float:
These two are used to store large decimal point numbers like 3.68, 4.567.
Declaration Example:
double floatval=3.68;
Inside Programming:
#include <iostream>
using namespace std;
int main ()
{
double floatval = 3.68; // Floating point number (with decimals)
cout << floatval; // output will be 1 as it's the true value
return 0; // terminate the program:
}
Contributed By: Shiva Patra
5) String:
It is used to store strings or text values and is also surrounded by double quotes.
There are more types of variables like null, void, etc.
Now, moving to the scope of the variables which means from where the variables will be accessible and can be manipulated. It decides the area of functioning of the variables. According to this property, the variables are classified in two types:
a) Local variables:
Local variables are variables whose existence and availability are valid inside the curly braces of the code in which it is declared. We can’t use the value of that variable outside the area of the braces.
Inside Programming:
#include <iostream>
using namespace std;
int main () {
int a, b; // Variables a and b are local variables declared here:
int result;
a = 16;
b = 14;
result = a + b;
cout << result;
return 0;
}
#include <iostream>
using namespace std;
int main () {
int a, b; // Variables a and b are local variables declared here:
int result;
a = 16;
b = 14;
result = a + b;
cout << result;
return 0;
}
Contributed By: Shiva Patra
b) Global variables:
Global variables are variables which can be used throughout the program once they are declared. They can be used in any class or by any function whenever needed. But, it should be very clear that they should be declared outside the main() function.
Inside Programming:
#include <iostream>
using namespace std;
int sum; // sum is global variable declared
int main () {
int x, y;
x = 34;
y = 56;
sum = x + y;
cout << sum;
return 0;
}
Static Variables:
Static Variables are one more kind of variable which once initialized they remain constant during function calls.
Seems like we covered much of the topic, get a reference from other sources too if you need extra information but I think it’s enough for someone to know about variables in C++.
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.