A Guide of C++ Variables

In this tutorial, you will learn variables in C++. C++ variable is a name or memory location in the C++ program. It defines to save a program’s input data and its computation results during the execution of a program. The value of a variable may change during the execution of a program. However, the name of the variables can’t be changed.

The variable creates in RAM(Random Access Memory). RAM is temporary memory in Computer where you can store variables temporarily. The data put in the variables are automatically removed when the program ends.

C++ Variables

Name of C++ Variable: It refers to an identifier that represents a memory location.
Address of Variable: It refers to the memory location of the variable.
Content Of Variable: It refers to the value stored in memory location referred to as the variable.

C++ Variable Declaration

The process of specifying the variable name and its type is known as a variable declaration. A program can have as many variables as needed. All the variables must be mentioned before they use in the program. A variable can be held anywhere in the program before its use.

The variable declaration gives information to the compiler about the variable. However, The compiler defines the declaration to determine how much memory is needed for each variable.
Example:

#include <iostream>
using namespace std;

int main() {
  int x = 15;
  cout << x;
  return 0;
}

Rules of Declaring Variables.

  • The first character of a variable must be a letter or underscore.
  • A variable can be declared only for one data type
  • Special symbols can not be used as variable names.
  • Both uppercase and lowercase are supported.
  • Blank spaces are not released in the variable name.

C++ Variables Initialization

Variable Initialization is a process of assigning a value to a variable at the time of declaration is known as variable initialization. The equal (=) sign is useful to initialize the variable like x=10 etc. The C++ compiler automatically sets aside some memory for the variable when it is declared. However, The memory location may contain meaningless data known as garbage value.

#include <iostream>
using namespace std;

int main() {
  int x = 5;
  int y = 6;
  int sum = x + y;
  cout << sum;
  return 0;
}

In conclusion, You need to learn C++ variables to become a developer.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top