C++ Variables

Data values are kept in variables as storage.

There are various sorts of variables in C++.

Here are a few of them:

  • Integers (whole numbers) without decimals, such as 63 or -1, are stored in integer variables specified with the keyword int.
  • Floating point numbers with decimals, such as 79.97 or -13.26, are stored in floating point variables specified using the keyword float.
  • Single characters, such “A” or “z,” are stored in character variables specified with the keyword char. Char values must be enclosed in single quotation marks.
  • When the bool keyword is used to define a boolean variable, it stores a single value that represents either false or true, 0 or 1.


A variable cannot be declared without its data type being mentioned. What we want to store in the variable and how much space we need it to hold will determine the data type of the variable. Declaring a variable has the following simple syntax:

data_type  variable_name;

Identifying a Variable

Anything can be referred to as a variable. However, there are several guidelines we must adhere to while naming a variable:

  • In C++, variable names can have any length between 1 and 255 characters.
  • Only alphabetic characters, numbers, and underscores (_) are permitted in variable names.
  • A digit cannot begin a variable.
  • A variable’s name cannot contain any white space.
  • Names of variables are case-sensitive.
  • No special characters or reserved keywords should be present in the name.

Variable Scope

A variable’s scope refers to the area of a programme where its presence is permitted. Variables can be divided into two categories according to their scope:

Local variables:

Local variables can only be evaluated from the specific function they are declared inside the braces of.

Global variables:

Global variables can be accessed from anywhere and are declared outside of any function.


#include <iostream>
using namespace std;
int a = 5; //global variable
void func()
    cout << a << endl;
int main()
    int a = 10; //local variable
    cout << a << endl;
    return 0;


Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.