Overview of Variables:

Variable are containers which are used to store data values. The type of data a variable is storing is the data type of the variable. The scope of a variable may be local or global in C++.

Scope:

  • Local Variables - declared inside the function.
  • Global Variables - declared outside the function.

Initializating a Variable:

dataType varName = dataValue;

Rules for assigning names to variables:

Names of variables are called identifiers. Use descriptive names for the variables to make the code readable.
  1. Length of name should be from 1 to 255 characters.
  2. Variable name starts with letter or underscore
  3. No space is allowed in the name of the variable
  4. Variable name contains only letters, underscores and numbers
  5. Variable name should not be a reserved keyword like "int", "main" etc.

Note : While naming variables, keep in mind that C++ is a case sensitive language.


variables.cpp
#include <iostream>
using namespace std;

int main(){
    int age=10;
    //while printing value of a variable, 
    //do not use double quotation marks.
    cout<<age;
    return 0;
}

You can also declare variable without assigning value and assign value later, like;

variables.cpp
#include <iostream>
using namespace std;

int main(){
    int age;
    age = 10;
    cout<<age;
    return 0;
}

You can also change the value of variable later in your code.

variables.cpp
#include <iostream>
using namespace std;

int main(){
    int age = 10;
    cout<<age;
    age = 20;
    cout<<age;
    return 0;
}

Another Example:

variables.cpp
#include <iostream>
using namespace std;

int main(){
    int age=20;
    cout<<"I am "<< age <<"years old.\n";
    return 0;
}

Operations on variables:

You can apply any mathematical operation on variables, but condition is that the data types of these variables must be compatible. You can not add int type variable to string. You can only add those variables whose data types are compatible.

variables.cpp
#include <iostream>
using namespace std;

int main(){
    int num1=10;
    int num2=5;
    int sum = num1+num2;
    int sub = num1-num2;
    int div = num1/num2;
    int mul = num1*num2;
    cout << "Sum " << sum << endl;
    cout << "Difference " << sub << endl;
    cout << "Quotient " << div << endl;
    cout << "Product " << mul << endl;
    return 0;
}

Other types of variables:

Like integer there are other different datatypes of variables.
  • Int stores numbers without decimal like 10, 20, 3000.
  • Float and double stores numbers with decimal like 2.1, 30.12 etc.
    The difference is that precision of double in 2x than float.
  • Char stores single character
  • String stores text (collection of characters) inside double quotes like, "My age is 20".
  • Boolean stores true or false.
    bool answer = true;
  • Void is used when you do not want to assign any type to variable.

You can declare variables of the same type in one line, like:

variables.cpp
#include <iostream>
using namespace std;

int main(){
    int a=10, b=5, c=3;
    cout << a + b + c << endl;
    return 0;
}

Constant Variables:

As name indicates, variable means “changeable”, but using const keyword you can make your variable unchangeable.

variables.cpp
#include <iostream>
using namespace std;

int main(){
    const int a=10; 
    // value of var a can not be changed
    a=20; // error
    return 0;
}

Printing different types of Variables:

variables.cpp
#include <iostream>
using namespace std;

int main(){
    int a = 10;
    float b = 10.5;
    double c = 20.3;
    string name = "John";
    char ch = 'c';
    bool isTrue = true;
    cout<<"a = "<< a <<endl;
    cout<<"b = " <<endl ;
    cout<<"c = " <<endl ;
    cout<<"name = " <<endl ;
    cout<<"ch = " <<endl ;
    cout<<"isTrue = " <<endl ;
    return 0;
}

Styles for naming variables:

Camel Case:
In camel case convention, we start the name with a small letter and if there are more than one words, we capitalize the first letters of the others words. For example, myAge, myName, johnMarks.
Pascal Case:
In pascal case, we capitalize the first letter of every word. For example, MyAge, MyName.
Snake Case:
In snake case, we separate words by underscores (_). For example, my_age, my_name.


We have already learnt about the primitive datatypes like int, double, float, string, char and bool etc. There are other datatypes. For example, derived datatypes like array, pointer and user defined datatypes like class, structure, enum etc which are not in scope of this chapter.