Variables in C++

Variables are the name to memory space in the computer where we can store data of certain types. They are important component of any programming language. In C++, each variable has its type which defines the type of value that can be stored by the variable, size taken by that variable and its range. Some basic types of variables in C++ are int, float, bool, char, etc.

  1. Variable Declaration in C++
  2. Rules for Naming Variable
  3. Data types of Variables
  4. Pointer Variables
  5. Reference Variables
  6. Scope of a Variable

Variable Declaration in C++

In C++, variables must be declared before using it. Declaring a variable tells the compiler that a variable of certain type is being used in the program. Using a variable before declaring it will cause an error. Variables can be declared in C++ as follows

datatype variable_name;

For e.g. int x;

Similarly, values can be assigned to variables as follows:

variable_name = value;

For e.g. x = 9;

The declaration and assignment statement can be combined into a single statement as follows:

datatype variable_name = value;

For e.g. int x = 9;

Any number of variables can be declared in a single statement also as follows:

datatype var1, var2, ... ,varN;

For e.g. char x, y, z, ... , m;

Rules for Naming Variable

  1. Variable name can’t be a C++ keyword. For e.g. int can’t be a variable name as it is a C++ keyword.
  2. Variable name must start with an alphabet (A-Z and a-z) or underscore ( _ ) sign. For e.g. var, X, _name, etc are valid variable names but 1a, $age, etc are invalid variable name.
  3. Variable names can have alphabet (A-Z and a-z), underscore ( _ ), digits (0-9) but can’t have other symbols such as %, &, @ , etc. For e.g. a_01, findSum are valid variables name but name&, calc% are not allowed in C++.
  4. The name of variable can be of any length but only first 31 characters are significant.

Some valid variable names are: db_password, _age, calcPercent, x, etc.

Some invalid variable names are: 1user, %1age, v@lue, !!, *name*, etc.

Data types of variables

Data types defines what type of data a variable can store. In C++, data type of a variable should be defined during declaration. The declaration statement informs the compiler about the type of data the variable can hold and memory required to store it. The basic data types available in C++ are

  • bool: It holds boolean value i.e. true(1) or false(0).
  • char: It holds character such as alphabet, digits, special characters.
  • wchar_t: It can hold 16 bit wide characters and are used to represent languages that have more than 255 characters. Example, Japanese.
  • int: It holds integer values such as 5, -100, 359, etc.
  • float: It holds floating point numbers such as 101.56, 200.00, -4.589, etc
  • double: It also holds floating point numbers but have more precision than float.

Modifiers such as signed, unsigned, long and short can be used before int and char data types to modify them according to our need. For example, if we are writing program to check if a number is prime or not, we can declare the number as unsigned int since prime numbers are always positive integer.

The following table shows the size and range of basic data types used in C++.

Data type Size (Bytes) Range
bool 1 0 to 1
char 1 -128 to 127
unsigned char 1 0 to 255
signed char 1 -128 to 127
wchar_t 2 0 to 512
int 2 -32768 to 32767
unsigned int 2 0 to 65535
signed int 2 -32768 to 32767
short int 2 -32768 to 32767
unsigned short int 2 0 to 65535
signed short int 2 -32768 to 32767
long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
signed long int 4 -2147483648 to 2147483647
float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 to 1.1E+4932

Range of a data type = 0 to 2n-1 (for signed) and 2n-1 to 2n-1-1 (for unsigned and with no prefix).

Note: The size of data type can be known by using sizeof() operator as sizeof(float). Some modern compilers such as CodeBlocks shows the size of integer as 4 bytes. However according to ANSI C++ standard its size is 2 bytes only.

Pointer variables

Normal C++ variables stores data and occupy certain memory in the computer. Pointer is a variable that store the address of other variable of same type i.e. a integer pointer will store the address of integer variable. Pointer can be declared using ‘*’ sign as,

datatype * ptr;

For e.g. float *fptr;

Assigning address to pointer variables

ptr = &variable_name;

For e.g. int x = 5;
int *p = &x; // address of x value of p

Reference variables

A reference variable is an alternative name for a previously defined variable. A reference variable must be initialized at the time of declaration. For example, if a is made reference of b, then a and b can be used interchangeably to represent that variable.

Syntax for creating reference variable

datatype &reference_variable = variable_name;

For example,

int xyz = 10;
int & abc = xyz;

Here abc is a reference variable for xyz. abc can be used as alternative for xyz. Both refer to the same data in memory. If value of one variable is changed, value of another variable is also changed.

For example,

abc = 5;

This statement will change the value of both abc and xyz to 5.

Scope of a Variable

Scope is a certain module of a program such as function, class or namespace. A scope of a variable is a region in which it is visible and can be accessed. Based on the scope of variable, a variable is of two types:

  1. Local variables
  2. Global variables

1. Local variables

Variables that are declared inside a module and can only be used within the module are called local variables. They can’t be used outside the block in which they are declared.

Example 1: C++ program to create and use local variable

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
    int a,b;  // local variables
    a=5;
    b=6;
    cout<<"a*b = "<<a*b;
    getch();
    return 0;
}

Output

a*b = 30

2. Global variables

Variables that are declared outside all modules and can be accessed and assigned by all the functions in the program are called global variables. The life time of a global variable is same as the life time of the program where it is declared.

Example 2: C++ program to create and use global variable

#include<iostream>
#include<conio.h>
using namespace std;

int g;    //  global variable

void test()
{
    cout<<"Inside test"<<endl;
    cout<<"g = "<<g<<endl;    
}

int main()
{
    g=25;
    cout<<"Inside main"<<endl;
    cout<<"g = "<<g<<endl;
    test();
    getch();
    return 0;
}

Output

Inside main
g = 25
Inside test
g = 25