Variables in C++

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:03:00

What are Variables in C++?

A variable is a data storage location in memory that has been named. Variables can have data types of int, float, boolean, char, or double.

Declaration of Variables in C++

Variables are declared in C++ by giving the data type first, then the variable name, optionally followed by an initial value, and finally a semicolon.

Variable initialization in C++

This can be done in two ways:

  1. Initialization occurs at the time of declaration.
  2. Initialization occurs after the declaration.

Rules for Naming a Variable

  • Names can be made up of alphabets (upper and lowercase), numerals, and underscores (_).
  • The initial character must be an alphabet or an underscore.
  • Variable names are case sensitive, therefore num and Number are different variable names.
  • The variable names must not contain any white spaces or special characters such as! or *.
  • A keyword cannot be used as a variable name.
  • After declaring a variable, its type cannot be modified.

Scope of Variables 

A scope is a program region, and there are generally three places where variables can be declared: ​

  1. Local variables are defined as variables that exist within a function or block.​
  2. In the definition of function parameters, also known as formal parameters.​
  3. Global variables are defined as variables that exist outside of any function.

Types of Variables in C++

Following are the Types of Variables in C++

  1. Local Variable
  2. Instance Variable
  3. Static Variable
  4. Automatic Variable
  5. External Variable


Local variable in C++

Local variables are declared and initialized at the beginning of a function or block, and memory is allocated inside its execution scope. Only statements within that function can access the local variable. Such variables are removed when the control exits the function.

Instance variables in C++

Instance variables are non-static variables defined in a class outside of constructors, methods, and other blocks. Their memory is allocated when an object of the class in which they are declared is created, and it is destroyed when the object is destroyed. Their initialization is optional when declaring; by default, they will contain garbage values. 

Static variables in C++

These are identical to instance variables but apply to all objects in the class. Each class has only one copy of static variables, which are specified using the static keyword. Their memory is allotted at the start of the program and destroyed when it is terminated.

Automatic Variables in C++

All local variables are automatic by default. They are also referred to as automatic variables.

External Variables in C++

External or global variables are declared with the extern keyword. You can declare an external variable several times, but the value can only be assigned once. Their default value is zero or null.

Self-paced Membership
  • 24+ Video Courses
  • 850+ Hands-On Labs
  • 500+ Quick Notes
  • 225+ Skill Tests
  • 120+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this