Variables in C++

A variable is a unique name that is used in a program to store data. A variable can be defined in a program by using the combinations of letter, numbers and underscore.

Rules For Declaring Variable

To create a variable in C++, you must follow the following rules:

  • A variable name cannot start with a digit.
  • A variable name can start with an alphabet or underscore.
  • A variable name is case sensitive, meaning variable names “hello” and “Hello” are two different variables. The uppercase and lower case characters are different.
  • A variable name cannot be C++ keywords like for, this, if, else, while, do, char, this, etc.
  • The name of a variable should be unique, readable, and easy to understand.

How to Declare Variables?

Variables must be declared first before using them in the programs. When we declare a variable, it tells the compiler that a variable of a certain type is being used in the program.

The syntax for declaration of a variable is as follows:


datatype variable_name;
Example

int x; // Here, x is variable name and int is the type of data x can store
double y; // Here, y is variable name and double is the type of data y can store
bool z;  // Here, z is variable name and bool is the type of data z can store   

How to Initialize Variables?

In C++, a variable can be initialized during the time of declaration. The syntax for initialization of variables in C++ language is:


datatype variable_name = value;
Example

int x = 100; // Here, default value of x is 100
double y = 350.90; // Here, default value of y is 350.90
bool z = true;  // Here, default value of z is true   

Types of Variables

  1. Local Variables — Local Variables are defined within the function, method or constructor. These variables are created when entered into the function and destroyed after exiting from the function. The scope of the variable exists only inside the function.
  2. Example
    
    int main()
    {
      int x = 2; // local variable
    }  
    
  3. Global Variables — Global Variables have a global scope. They are declared outside the function. These variables can be accessed by all the functions i.e. both local function and global function, and exist till the end of the program.
  4. Example
    
    int y = 10; // global variable
    
    int main()
    {
      int x = 5; // local variable
    }
    
  5. Static Variables — Static Variables are declared with the static keyword. When a variable is static, the space in memory for that variable is allocated only once throughout the entire program. A static variable exists till the end of the program once it is declared in the program.
  6. Example
    
    #include <iostream>
    using namespace std;
    
    void myfunc()
    {
      static int x = 2;
      cout << "Value of x: " << x << "\n";
      x++;
    }
    
    int main()
    {
      myfunc();
      myfunc();
      myfunc();
      return 0;
    }
    
  7. Automatic Variables — Automatic Variables are declared with the word auto. When a variable is declared using the auto keyword, the C++ compiler automatically detects and assigns the data type to that variable based on the value assigned to it. All the variables inside the function are default considered as an automatic variable.
  8. Syntax
    
        auto variable_name = value;
    
    Example
    
    int main()
    {
      int x = 20; 
      auto y = 12;
    }
    

    Here in the above example, variable y is automatically assigned int data type because its value is an integer.

  9. External Variables — External Variables are declared with the extern keyword and are defined outside the function. The external variables are accessible globally and therefore these variables are also known as global variables.
  10. Syntax
    
        extern datatype variable_name; // variable with extern keyword
        extern datatype function_name(); // function with extern keyword
    
    Example
    
    #include <stdio.h>
    extern int x = 100;
    
    int y = 2;
    
    int main()
    {
      printf("Value of extern variables x and y : %d, %d\n", x, y);
      return 0;
    }