Variables in C++

Variables in C++
Techiio-author
Written by Sagar RabidasJanuary 27, 2022
5 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion on Variables in C++

Variables in C++

A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Rules and Regulations for Defining Variables in C++ Language

  • Variables can be a mixture of digits, special characters like percent (&), underscore (_), or string.
  • Upper and lower cases are treated as different variables as C++ is case sensitive language.
  • C++ variables must be started with the character. It will not consider the number as a first character. 6educba is not a valid variable because it starts with the number whereas educba6 can be a valid variable as it started with the character.
  • variables in C++ language should not be a keyword. for, this, if, else, while, do, char, this, etc are the keywords that are used for the specific purpose. These keywords cannot be used as a variable in C++.
  • Blank spaces are not allowed for the variables.

How do Variables Work in C++ Language?

  • Declaration of variables informs the compiler of the types of data variables that will be used in the program.
  • Declaration of variables names informs the compiler of the name of the variables that are used to store the value in the program.
  • While declaring variables I tell the compiler the storage that variables need. The compiler does not have to worry about the storage until it is declared.

Types of Variables in C++ Language

There are 5 types of variables in the C++ language which are as follows:

Local Variables

Local variables are declared inside the function. Local variables must be declared before they have been used in the program. Functions that are declared inside the function can change the value of variables. Functions outside cannot change the value of local variables.

Here is an example

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

Global Variables

Global variables are declared outside the functions. Any function i.e. both local function and global function can change the value of global variables.

An example is given as follows,

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

Static Variables

These variables are declared with the word static.

An example is given as follows,

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

Automatic Variables

Automatic variables are declared with the auto keyword. All the variables that are declared inside the functions are default considered as automatic variables.

An example is given as follows,

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

External Variables

By using the extern keyword, external variables are declared.

extern z = 4; //external variable

C
C plus plus
Variables in C
Variables
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance