Variable in c++ language

  • It is a name of storage space which is used to dtore data.
  • I is value is changable.
  • It always contains last value stored to it.
  • It is always declared with data type.

Variable Declaration

int rollno;
flaot marks;
char grade;

Here rollno is a variable of type int,marks is a variable of type float and grade is a variable of type char.

Variable Initialization

int rollno=07;
flaot marks=80.5;
char grade=='A';

Here 07 is the value of rollno,80.5 is the value of marks and A is the value of grade.Character value is always written in single quotes.

Rules to declare a variable

  • The first letter of a variable should be alphabet or underscore(_).
  • The first letter of variable should not be digit.
  • After first character it may be combination of alphabets and digit.
  • Blank spaces are not allowed in variable name.
  • Variable name should not be a keyword.

Valid variable names:

int num;
int _number;
int numbers;

Invalid variable names:

int 4;
int num number;
int char;