Introduction to Variable in CPlusPlus (Declaration & Scope)

Introduction to Variable in CPlusPlus (Declaration & Scope)

Programming is not just writing some lines of codes to print a statement like “Hello World!”. Indeed, it is much complex. In order to write programs that are really useful and help us save time, we need to go a little far and introduce the concept of variable.

Variable is a name given to a specific memory location in the storage device which the program can manipulate. Variables can store different data types and the value stored in it can change during the execution of the program.

A variable name can be made up of letters, digits and underscore. Upper case and lower case letters are also distinct in C++. As C++ is a case sensitive programming language.

Basic Types of Variables in CPlusPlus:

Basic Variable in C++

The data types which are given to variable while their declaration in the program actually specifies the size of the memory for that variable. For example when you define a variable as (char) it will only occupy 1 byte of memory. But if you define the same variable as (int) it will occupy 2 bytes or more. There are further larger types of data types too. Such as double and float and etc.

Some of the variable types in C++ are described below:

No. Variable Description
1 bool Stores true or false value
2 char Stores only one Alpha-Numeric letter
3 int Stores whole numbers
4 float Stores a decimal point number
5 void It is NULL and doesn’t store anything

Note: CPlusPlus defines further complex types of data too. They are such as pointer variables, arrays, references, data structures and classes.

See Also:   Data Types in Programming Languages

How to Declare a Variable?

A variable in C++ can be declared at any point of the program. But it should be declared before its use in the program. Following is the syntax of How you can declare a variable;

You can also initialize your variable at the same time of declaration.

If you do not initialize your variable at the time of declaration; the system will give it a garbage value also called the default value.

What is important in declaring a variable in C++?

  • The name of variables should either start with an alphabet or an underscore (_).
  • The variables name cannot have any space in it.
  • The name of the variables should not match any keyword of the language.
  • No other symbol except (_) can be used in the name of the variables.

Some Examples;

A Program to demonstrate the concept of Variables:


See Also:   Array Data Structure

Scope of the Variables:

Every variable has a boundary of functioning. Out of that boundary the variables is of no use. This boundary is called the scope of the variables. According to the scope of variables, they are divided into two major categories.

Global Variables:
It is a variable which is declared or even initialized outside the main () function. It can be accessed by any function or class in the program. Even if it is initialized at the time of the declaration, you can reassign it with any other value.

Local Variables:

It is a variable which is declared inside a function or a class and can only be accessed by the function or class (inside a pair of curly brace). Out of that the variable does not mean anything.

Previous Web Design Tutorials: HTML5 Elements
Next How to Use Macros in Office 2016

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like