info@tutsfinder.in



Variables


Variables are used to store data, it tell the compiler the nature of the data and compiler reserves the necessary space for it.


Rules to define variables in C++

1. A variable contains only alphanumeric characters(a-z , A-Z , 0-9) and underscore(_).

2. The first character of a variable should start with alphabet(a-z , A-Z) or underscore (_).

3. Variables are also case sensitive.

4. Keep variables name in small alphabets


Syntax – type variable_name;

For Example – int x ,float y


Following example will help you to understand what variables are.

Assume you are writing a program to addition of two numbers. The user has to enter some input for that operands, user needs some memory area to store that two input values, therefore we named that two variables a and b and store in another variable sum so these memory names is known as variables.


Program to show addition of two numbers where we will take 3 variables, let’s see how it works



#include <iostream>
using namespace std;

int main() {
    int a,b,sum;
  cout << " Enter first number ";
  cin >> a;
  cout << " Enter second number ";
  cin >> b;
  sum=a+b;
  cout << " Sum of a & b is " << sum;
  return 0;
}


Output :



Enter first number10
 Enter second number5
 Sum of a & b is 15