Variables in C++

Video Tutorial
FREE
Variables Intro thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Variables in C++ are the foundation of programming, serving as named memory locations for storing data values. Essential to any C++ program, these C++ Variables allow for the easy management and manipulation of data by providing a means to access and modify the values stored in memory. Understanding the declaration, scope, and types of variables is crucial for effective programming. This article will delve into the rules governing variables in C++, how they are interpreted by the compiler, and explore the concept of variable scope, highlighting the indispensable role of variables in C++ programming.

How to Declare Variables in C++?

Syntax:

  • datatype: Specifies the type of the variable, such as int, float, etc.
  • variable_name: Represents the variable name.
  • value: Indicates the initial value held by the variable var of type datatype.

Example:

This is the integer variable with the name days and holds value 5.

We can declare single or multiple variables.

Single Variable Syntax:

Multiple Variables in a Single Line Syntax:

Example:

Rules for defining Variables in C++

  1. Variable name should contain only letters, digits, or underscore(_).
  2. Variable name should start with a letter or underscore only. It cannot start with a digit.
  3. Variable name should not contain any white spaces or characters like !, #, %, etc.
  4. Variable names are case sensitive i.e age and Age are two different variables.
  5. Keywords or Reserved words cannot be variable names.
  6. Some valid variable names are age, length and car_color; some invalid variable names include 1var (starts with a digit), mother age (contains white space), and int (keyword).

Declaring Vs Defining Variables in C++

DeclarationDefinition
1. It tells the compiler about the name and data type of the variable only.1. It tells the compiler about the name and data type of the variable as well as the memory location of the variable.
2. We can declare a variable multiple times.2. We can define a variable only once.
3. Example:
extern int age; 
This declaration tells the compiler that the variable age is declared but the memory for variable age will be defined later in the same file or a different file.
3. Example:
 int age = 24;  
This variable definition tells the compiler that the variable age is declared and it is used for allocating memory for the variable.

Types of Variables in C++

Local variable in C++

  1. These variables are defined within a block, function, or method.
  2. They are accessible within that block and get destroyed when the block ends, i.e. memory assigned to the variable is released.
  3. Initialization of this variable type is mandatory, if we don't do it, the compiler does it and gives it a garbage value.

Example:

Instance variable in c++

  1. These are non-static variables declared in a class outside constructors, methods, and other blocks.
  2. They get memory when the object of that class in which they are declared is created and destroyed when the object is destroyed.
  3. Their initialization is not compulsory while declaring; by default, they will have garbage values.
  4. Every object of the class gets a separate copy of their instance variables.
  5. The scope of the instance variable is controlled by the access specifier, which is the class's built-in functionality.

Example:

Static variable in C++

  1. These are similar to instance variables but common to every object of the class; in other words, there is only a single copy of a static variable per class, and static variables are declared with a static keyword.
  2. They get memory at the program's start and get destroyed when it ends.
  3. The static variables are stored in the memory data segment.
  4. Their initialization is compulsory and done outside the class; by default, the int variable will have a 0 value, and the boolean will have a false.

Example:

Automatic variable in C++

  1. C++ Variables declared with auto keyword are automatic variables.
  2. Auto keyword specifies that the type of the variable that is being declared will automatically be deduced from its initializer.
  3. The difference between normal and auto variables is that the user has to specify the datatype for the normal variable. With auto keyword, the compiler automatically deduces the datatype.

Example:

External variable in C++

  1. C++ Variables declared with extern keyword are external variables.
  2. It is used when a particular file needs to access a variable from another file i.e. if there is a variable defined in another file, let's say file_1 and we are writing a program in another file, i.e. file_2, then we can use the variable defined in file_1 in our file_2 by using extern keyword and including header file of file_1 in file_2.
  3. It is used for variable definition only i.e. variable declared with extern keyword does not get any memory.

Example:

Instance Variable Vs Static Variable in C++

Instance variableStatic variable
1. Declared in a class outside constructors, methods, and other blocks.1. Similar to instance variables, they are common to every object of the class and are declared with a static keyword.
2. They get memory when the object of that class in which they are declared is created and destroyed when the object is destroyed.2. They get memory at the start of the program and get destroyed when the program ends.
3. Every object of the class gets a separate copy of their instance variables3. There is only a single copy of static variable per class i.e static variables get memory only once and not again and again when different class objects are created.
4. Their initialization is not compulsory.4. Their initialization is compulsory and it is done outside the class.
5. To access the instance variable we require an object reference of that class.5. A static variable could be accessed using the class name.

Conclusion

In this article, we learned about:

  1. C++ Variables store data and can be of many types like int, float, etc and user-defined variables are also present.
  2. Value can be assigned to a variable at the time of variable definition; otherwise, garbage value is assigned if we do not assign value.
  3. Difference between variable declaration and definition. The declaration specifies the variable's data type and names, whereas the definition allocates memory too.
  4. Working of variables in C++ where the variable type specifies memory space needed by the variable.
  5. Rules for defining variables that specify what variable names are allowed by the compiler.
  6. The variable's scope is of two types: global and local.
  7. Types of variables are instance, static, automatic, and external.