x

C++ Variables

PREV     NEXT

What are variables?

The variables are basically used to store the values which can be modified later on in the program. The variables can be declared in many ways and every way with different memory requirements. The variable is used to represent a name of memory location that is allocated by the compiler depending upon the data type of the variable. The variable defined within a program has a specific type which determines the size and the layout of the variable memory. At the time of declaration the variables are composed of letters, digits and an underscore character. It can start with either a letter or an underscore. In C++, the uppercase and lowercase letters are unique because C++ is case sensitive.


For example :

How to define variables in C++?

As you define the variable it let the compiler know that how much and where to create the storage for the variable. They are declared before they are taken in use but in C++ you can declare them in the middle of the program but before using them. The variable defined in the program specifies the data type and contains the list of one or more variables of the same data type.

Let us have a look at the syntax:

type variable-list;

In this syntax, type is a data type like int, char, float, double, etc. and variable-list is that which contains one or more identifiers names separated by commas. Like

A variable declaration is also useful when you are using multiple files and you define your variable in one of those files which will be available at the time of linking of the program. So, in this case extern keyword can be used to declare a variable at any place.


How to initialize variable in C++?

In this the variables are initialized and the initial value can be assigned along with the declaration. As you can see in the above examples the variables are declared but not initialized with any value.

 Let us have a look at the example:

What are the rules to declare variables?

  • First character must be letter or underscore.
  • Keywords in C++ cannot be used in variable name.
  • Variable names are case sensitive in C++.
  • It can consist of capital letter A-Z, lowercase letters a-z, digits 0-9 and underscore character.
  • You cannot use blank spaces in variable names.

Scope of Variables in C++

The variables have a boundary of functioning and out of that boundary they don’t hold their value and this boundary are known as scope of variables. In most of the cases it’s between the curly braces where the variable is declared and it exists but not outside the curly braces.

The variables can be divided in to two main types.

They are:

  • Local variables
  • Global variables

Local Variables

The local variables are those variables which exist in between the curly braces only where they are declared. But outside the curly braces they are unavailable and lead to compile time error.

Let us have a look at the example:

Global Variables

 The global variables are those variables which are once declared can be used throughout the program by any class or function. They are declared outside the main function.

Let us have a look at the example:

PREV     NEXT



Like it? Please Spread the word!