All About Variables in C++

Variables in C++:

The process of creating variable name with its data type is called variable declaration. In C++ it is compulsory to declare the variable with its data type.

Syntax of any variable in C++ is:

                 data_type variable_name;

data_type:             It indicates which type of data can be stored in specific variable.

Variable_name:    It refers to the memory location in system.

 Examples of variable declaration:

  int number;

  float size;

  double price;

  char name;

Important Rules for declaring variables:

  • Any variable may use letters , numbers and underscore( _ ) .
  • Both capital and lower letters are allowed in C++.
  • A variable can only declare the one data type.
  • Special Symbols like(%,#,^,*) cannot be used in variable names.

Variable Initialization in C++:

The process of assigning value to the variable is called variable initialization.

The C++ compiler automatically sets aside some memory for the variable when it is declared. The memory location may contain meaningless data known as garbage value. It may produce unexpected results in some computations. All variables should be initialized to avoid this problem.

Syntax

The syntax of initializing a variable is as follows:

              type_name variable = value;

 

type_name:       It indicates the data type of the variable to be initialized.

variable :            It is name of the variable to be initialized.

= :                        It is the assignment operator used to initialize a variable.

Value:                 It is the value to initialize a variable.

Examples:

int marks = 123;

char name=”Hamza_Arshad”;

Share: