Variables and types

0 maypontius17573 November 13, 2020

Variables are the symbolic name that is using for the store data item, i.e. a numerical quantity or a character constant. Variables are defined in a computer to represent an item of data input by the user,  any intermediate calculations, or the ends results. Unlike constant, Value of variables can change during the execution of the program. The same variable can store different values at different portions of a program.

Variables name may consist of letters, digits, or underscore characters.

Variable Declaration:

Any program should define before using it in a program. The variables are defined or declared using the following syntax:

data-type variable_name = value;

For Example:

int num = 20;
float num = 20;

Rules For Variable Declaration:

  1. The variable name should be started with only letters or underscore.
int ab;       //valid
int 1a;      //invalid

2. The variable name should not be a keyword.

int if;           //invalid as if as keyword
init float;   //invalid as float as keyword

3. White spaces are not allowed between characters of variables, but underscores are allowed.

float age of students;      // invalid
float age_of_student;     // valid

4. The variable name is case sensitive, i.e uppercase and lowercase are different. Thus variable temp is not the same as Temp or TEMP.

5. No two variables of the same name are not allowed to be declared in the same scope.

Types of Variable:

Here we have variables category on two bases. Firstly we categorized on the basis of Data Types:

  • int: Variables that hold an integer value
  • char: hold characters such as ‘A’, ‘b’, ‘V’
  • bool: Store a boolean value i.e true or false
  • double: hold a double-precision floating-point value
  • float: Store double-precision floating-point value

Next, We have categorized variables on the basis of scope.

  • Global Variables
  • Local Variables

Global Variables:

The global variables are those variables that are using outside the function and can access throughout the program. Let’s take an example to clarify this statement.

#include <iostream>
using namespace std;

// This is the global variabe that is declared outside the function
char chp = 'Y';

int main(){

    cout << "Value of chp => " << chp << endl;

    //Now we assign another value to chp
    chp = 'N';

    cout << "Value of chp => " << chp << endl;
}

The output of the above program is:

Value of chp => Y
Value of chp => N

In the above program, We have declared a variable chp outside the function but also we access that two times without any issues.

Local Variables:

Local Variables are those variables that are declare inside the user-defined function, main loop, or any control statement that have limited score inside the braces. Let’s take an example to clarify this statement.

#include <iostream>
using namespace std;


char local_variable(){
    // This is the local variabe that can be access inside this function only
    char chp = 'Y';
}

int main(){

    cout << "Value of chp => " << chp << endl;
    return 0;
}

If we compile the above program then we will get an error because we have declared the chp as a local variable so that we can use chp variable inside the local_variable function only. If we need that variable in the main function also, we have to declare inside the main function separately or we can declare the variable globally.