Variables in C++ - The Coding Shala
Home >> Learn C++ >> Variables in C++
or
Variables in C++
Variables are one of the most important parts of every programming language. Variables in C++ are used to store the data that are going to use throughout the program. Every data is going to store at some memory location so variables are just the name of that memory location. Using variables in C++ we can manipulate data stored at the memory locations. These are following basic types of variable in C++ -
- bool
- char
- int
- float
- double
- void
- wchar_t
Variable Declaration and Initialization in C++
We can declare variables in C++ at the starting of the program or anywhere in the program but must be declared before they are used. For variables declaration we have to use valid data types, based on the datatypes compiler creates storage for the variable. The syntax of variables declaration in c++ is as follows -
Datatype variable_name;
or
DataType variable1,variable2,..;
After the declaration, the next part is to initialization of variables. Initialization means assigning values to the variables. We can declare and assign the value to them in a single step as well in two steps. The following c++ program shows declaration and initialization of variables in c++ -
#include<iostream> using namespace std; int main(){ int i; //declare of variable i int j,k,l; // declaration of variables j , k , l; i = 12; // initialization of i char s = 'p'; // declaration and initialization of variable s cout<<"This is variable i with value " << i<<endl; cout<<"this is variable s with value " <<s<<endl; return 0; } Output >> This is variable i with value 12 this is variable s with value p
Rules for Variables naming in C++
We all know how hard it is to name a variable(according to the memes :p ) so here are the rules for the naming of variables in c++ -
- Variables in c++ can have alphabets, digits and underscore.
- Variable names in c++ can’t start with the digit.
- Variable names in c++ can start with alphabets and underscore only.
- We can’t use reserved keywords as the variable name.
- White spaces are not allowed with the variable names in c++.
Apart from these rules always try to use a sensible and informative name of the variable. It will help in debugging and for later uses in the program.
Lvalues and Rvalues
Lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address). In simple terms l-values refers to the memory location which identifies an object. L-value can appear as either the left hand or right-hand side of an assignment operator(=).
Rvalues are defined by exclusion. In simple terms, r-value refers to the data value that is stored at some address in memory. An r-value can appear on the right-hand side but not on the left-hand side of an assignment operator(=).
The following is an example of l-value and r-value -
int i = 10; // i is l-value int i; 10 = i; //gives error
This is because i has an address in memory and is a value. While 10 doesn’t have any identifiable memory location and hence is an rvalue. The followings are some example of lvalue and r-value -
int a; // a is a l-value a = 10; int b = a; //a and b both are l-values 10 = a; //gives error a +1 = 10; //Error
Scopes of variables in C++
Variables in c++ have their own area of functioning and if we use them out of that boundary will get errors, this boundary is called the scope of the variables. For most of the time, this scope is defined by curly braces and based on the scope we can broadly divide variables into two main types -
- Global Variable
- Local Variable
Global Variables in C++
Global variables are those which declared outside the main() function and can be used throughout the lifetime of the program by any class or function. We can declare as well initialized the global variables.
Local Variables in C++
Local variables are the variables which exist only between the curly braces, in which it is declared.
The following is the example of global and local variables in c++ -
#include <iostream> using namespace std; int a,b=10; //these are global variables int main() { cout<<"global variable b : "<<b<<endl; a = 5; b = 15; cout<<"global variable a and b are : "<<a<<" and "<<b<<endl; int c=5; //local variable cout<<"local variable c "<<c<<endl; return 0; } Output >>> global variable b : 10 global variable a and b are : 5 and 15 local variable c 5
Other Posts You May Like
Comments
Post a Comment