The Wayback Machine - https://web.archive.org/web/20250118185048/https://www.geeksforgeeks.org/cpp-variables/
Open In App

C++ Variables

Last Updated : 19 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution. In this article, we will learn about the variables in C++ and how to use them in our programs.

Let’s take a look at an example:

C++
#include <iostream>
using namespace std;

int main() {
    
      // A variable that may store the data of
      // int type (whole numbers)
    int var;
  
      // Assigning a whole number
      var = 100;
  
      // Printing the variable
      cout << var;
  
    return 0;
}

Output
100

Explanation: A variable with name var is created. It is of the type int (keyword for integer) and can store the whole numbers. In the next step, we assigned a value 100 to it and finally, we access the stored value using the variable’s name.

The C++ Course covers the types of variables in C++ and how to use them effectively in your programs.

Syntax of Variables

Creating a variable and giving it a name is called variable definition. The syntax of variable definition is:

type name;

Here,

  • type: type of data that a variable can store.
  • name: name assigned to the variable.

Multiple variables of the same type can be defined as:

type name1, name2, name3 ….;

Types of Variables

In C++, following are the data types a variable can store:

  • int: For storing integers or whole numbers.
  • float: For storing real numbers with decimal points.
  • char: For storing single characters.
  • bool: For storing boolean value i.e. true and false.
  • string: For storing the textual data.

Rules For Naming Variable

The names given to a variable is called identifiers. There are some rules for giving a name to the variable:

  • The name of the variable contains letters, digits, and underscores.
  • The name of the variable is case sensitive (e.g. Arr and arr both are different variables).
  • The name of the variable does not contain any whitespace and special characters (ex #, $, %, *, etc).
  • All the variable names must begin with a letter of the alphabet or an underscore (_).
  • We cannot used C++ keyword (e.g. float, double, class) as a variable name.

Some valid variable names: x, var, _here, arr40
Some invalid variable names: 89, a b, double

Variable Initialization

The next step is initialization. It means assigning an initial value to the variable. It is done by using an assignment operator = with the variable name as left operand and the value as the right operand.

type name = value; // At the time of declaration
name = value; // After declaration
type name1 = value1, name2 = value2; // For multiple variables

The value should be of the same type as variable. For example, a variable declared as int var; should only store the integer variable.

Examples of Variables

The below examples demonstrate how to use the variables in our C++ programs:

Store Data in a Variable and Print It

C++
#include <iostream>
using namespace std;

int main() {

    // Defining and initializing a variable
      int age1 = 15;
  
      // Defining a variable
      int age2;
  
      // Initialize the variable
      age2 = 99;
  
      // Displaying variable
      cout << age1 << endl;
      cout << age2;
    return 0;
}

Output
15
99

Create Multiple Variables at Once

C++
#include <iostream>
using namespace std;

int main() {
  
      // Creating three variables that store characters
      char a = 'A', b = 'B', c = 'C';
  
      // Update the value
      c = 'c';

    cout << a << " " << b << " " << c;
    return 0;
}

Output
A B c

Memory Management of Variables

When a variable is declared, the compiler is told about the variable name and the type of data it will store. It is not allocated the any memory at this point. It is when the variable is defined it is allocated the memory. The amount of memory allocated depends upon the type of data that a variable intends to store.

Initially, the variable only stores some garbage value (value that does not mean anything). At the time of initialization, it is assigned some meaningful value that using the assignment operator that is then stored in the memory allocated to the function. So basically, we are manipulating a specific part of memory using the variable name.

At last, the data stored in the allocated memory can be accessed using the variable name.

Note: All the above syntax of creating a variable is variable definition as compiler automatically. To only declare a variable in C++, we can use the extern keyword. Refer to this article to learn more – Difference between Definition and Declaration



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg