C++ Variables Tutorial for Beginners with Examples

Variables in C++ act as memory locations; they are simply the names of the containers or elements that store the data or values that will be used later in the program for execution.

Complete list of fundamental types in C++

GroupType namesNotes on size/precision
Character typeschar

char16_t

char32_t

wchar_t
-Exactly one byte in size. At least 8 bits.

-Not smaller than char. At least 16 bits.

-Not smaller than char16_t. At least 32 bits.

-Can represent the largest supported character set.
Integer types (signed)signed char

signed short int


signed int


signed long int


signed long long int
-Same size as char. At least 8 bits.

-Not smaller than char. At least 16 bits.

-Not smaller than short. At least 16 bits.

-Not smaller than int. At least 32 bits.

-Not smaller than long. At least 64 bits.
Integer types (unsigned)unsigned char
unsigned short int
unsigned int
unsigned long int
unsigned long long int
(Same size as their signed counterparts)
Floating-point typesfloat
double
long double

-Precision not less than float
-Precision not less than double
Boolean typebool
Void typevoidno storage
Null pointerdecltype(nullptr)

Introduction to Variables in C++

Variables can be defined using a combination of letters, digits, or special symbols such as numbers and the underscore characters(_), as well as data types such as char, int, float, and double.

Variables can be anything except the reserved keyword, and their first letter must begin with the letter only.

The use of variables is among the most fundamental aspects of computer programming. Without variables, a programming language cannot be considered complete.

We can also declare that the program will not run if the variables are absent. C++, much like every other programming language, requires the use of variables in order for a program to run successfully.

Rather than being utilized to execute the program, variables are responsible for storing the value or string that is being used.

The program cannot be executed because it requires saving value. Because of this, variables are considered the most important part of the programming language.

Furthermore, the only words that cannot be used as variables are the keywords. To define variables, we must first identify the type of variable that will be used.

Any type, including int, double, char, float, long int, short int, long int, short int, etc., can be used. Integer values, such as 5, 19, 519, and 1000, can be stored in an int variable. A Char variable can hold a single character or string, such as “a” or “educate.”

In addition, Float is the data type used to hold float numbers such as 2.3, 3.679, and 9.45. To hold lengthy integer numbers, the long int data type is utilized.

In this piece, we will go over the process of declaring variables and initializing them in the C++ programming language. And the different categories of variables.

Rules and Regulations for Defining Variables in C++ Language

  1. Variables can be a mix of numbers, special characters like percent (%), underscores (_), and strings.
  2. As C++ is a case-sensitive language, uppercase and lowercase letters are treated as different variables. The variables educba and eduCBA will be treated as two separate things.
  3. Variables in C++ have to begin with a character. It won’t think of the number as the first letter. 6educba isn’t a valid variable because it starts with a number, but educba6 is because it starts with a letter.
  4. In C++, variables should not be a keyword. The – for, if, else, while, do, char, this, etc., are all keywords that are used for this specific purpose. These keywords can’t be used as variables in C++.
  5. There can’t be any blank spaces in the variables. Edu cba is not a valid variable because there is a space between edu and cba. However, educba is a valid variable, and so is edu cba, which uses an underscore to join the two variables.

How do Variables Work in C++ Language?

  1. When you declare a variable, you tell the compiler what kind of data it will hold.
  2. Declaring the names of variables tells the compiler what those variables are called that are used to store values in the program.
  3. When I define a variable, I tell the compiler how much space it needs. The compiler doesn’t have to worry about the storage until it is declared.

How to Declare Variables in C++ Language?

Before starting a program, you can declare variables. Here is the format for declaring a variable.

where data type = variable name;

As an example

int a;
float mynumber;

For example, cab is a variable name, and int is a data type. In the second example, we have set up two variables. 6.9 and 7.3 are variables, and float is a data type.

As soon as the variables are declared, the compiler sets aside space for them. This space will be used by the program.

Let’s have a look at the entire C++ code for the memory example from earlier in this chapter to see how variable declarations function in a program:

Program to illustrate the Declaration of Variables in C++ Language

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
  // declaring variables:
  int a, b;
  int result;

  // process:
  a = 5;
  b = 2;
  a = a + 1;
  result = a - b;

  // print out the result:
  cout << result;

  // terminate the program:
  return 0;
}

How to Initialize Variables in C++ Language?

In C++, variables can be set up by giving them their values when they are declared. In the C++ language, the syntax for setting up variables is: data type variable name = value;

One example is

int x = 1;
character b = “eduCBA.”

In Example 1, we gave the variable x the value 10 when we set it up using an equal sign. In Example 2, we set b to be a character with a value of eduCBA.

Program to Illustrate Initialization of Variables in C++ Language

#include<iostream>
using namespace std;
int main()
{
int x = 5, y = 15;
int z = x + y;
cout << "Sum of two numbers is: "<<  z;
return 0;
}

Types of Variables in C++ Language

1. Local Variables in C++

Inside the function, local variables are named. Before they can be used in a program, local variables must be declared. Variables can be changed by functions that are declared inside the function. However, local variables can’t be changed by functions that aren’t in the same file.

Example Program

int main()
{
int x = 2; //local variable
}

2. Global Variables C++

Outside of the functions, global variables are declared. The value of global variables can be changed by any function, whether it’s a local function or a global function.

Example Program

int y = 10; //global variable
int main()
{
int x = 5; //local variable
}

3. Static Variables C++

These variables are declared using the word “static.”

Example Program

int main()
{
int x = 5; //local variable
static y = 2; //static variable
}

4. Automatic Variables C++

The auto keyword is used to create variables that are automatically set. By default, an automatic variable is any variable declared inside a function.

Example Program

int main()
{
int x = 20; //local variable (Automatic variable)
auto y = 12; //automatic variable
}

5. External Variables C++

When the keyword “extern” is used, external variables are declared.

Example Program

extern z = 4; //external variable

Conclusion

In this guide, we have discussed the significance of variables in the C++ programming language, and with some examples, we have gone over how to work with variable.

In addition, we have examined five distinct varieties of variables available to users of the C++ programming language, complete with examples. I hope that you find this post informative.

Leave a Comment