C++ | Variables

In this article, we are going to learn about the variables in c++.


Variables are containers for storing data values.
A variable in simple terms is a storage place that has some memory allocated to it. A variable is used to store some form of data. Different types of variables require different amounts of memory.

Declaration of Variables:

To create a variable, specify the type and may assign it a value or we can assign the value later in the program:


type variableName; //Declaration of single variable
type varibleName1, variableName2; //Declaration of multiple variables

There are a set of rules that have to be followed for declaring variables in C++.

Rules for Declaring Variables:

A variable can have alphabets, digits, and underscore.
· A variable name can start with the alphabet, and underscore only. It can’t start with a digit.
· No whitespace is allowed within the variable name.
· A variable name must not be any reserved word or keyword, e.g., int, goto, etc.

Example: _hemanth, hemanth_kumar, hemanth51, hemanth_25 – all are valid variable names. 

Some examples of invalid variable names are – 

  1. Hemanth Kumar (there is whitespace between Hemanth and Kumar)
  2. 82hemanth (the name starting with digits)
  3. void, int, char (we cannot declare them as variables as they are reserved words or keywords in C++)

Initialization of Variables

In C++, there are three ways to initialize variables. 

See also  LOOPS IN C++

· The first one, known as c-like initialization (because it is inherited from the C language), consists of appending an equal sign followed by the value to which the variable is initialized:

Syntax:  type identifier = initial_value; 

For example, to declare a variable of type int called x and initialize it to a value of zero from the same moment it is declared, we can write: int x = 0; 

· A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses (()): type identifier (initial_value); For example: int x (0); 

· Finally, a third method, known as uniform initialization, is similar to the above but uses curly braces ({}) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011)

Syntax: type identifier {initial_value}; 

Example: int x {0};

Types of Variables in C++

In C++, there are different types of variables based on data types (defined with different keywords), for example:

· bool – Stores either value true or false.

· char – A char variable stores a character but internally it is an integer type.

· int – stores integers, without decimals, such as 120, 8, -50

· float – stores floating point values, such as 20.8,0.5

· double – stores floating point same as float but can store values much greater than the float limit

· void – Represents the absence of type.

· wchar_t – A wide character type.

In C++ we can also define various other types of variables like Enumeration, Pointer, Array, Reference, Structure, and Classes. 

See also  C++98 features

The above types are variables of different data types. There are also variables of different types on the bases of scope like:

· Local Variables 

· Global Variables

We will see about those in the coming articles.

NOTE: I have mentioned data types which we have learned in the previous article.

That is all for this article. Happy Coding.

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.