C++ Variables

In this tutorial, you will learn about C++ variables and the rules for naming a variable.


Variables are a core part of any program for data handling. Through variables, data can be taken as an input, processed, and displayed as an output. You need to know about the different variable types so that you can use the correct type required for your program. This will make your code memory efficient and is considered a good format for coding. Expecting a floating-point number as an input from the user but using an integer variable to store the input can also lead to errors in your program and hence the right type of data required the right type of variable to store it in.

Syntax

Following is the basic syntax of declaring a variable in C++.

variable_type variable_name

In C++, there are different types of variables to store different values as mentioned below:

  • short/int/long – Can only store integers without decimal points. Eg. 10, -30.
  • float/double – Can store decimal point values like 10.50. It can also store integer values but it appends trailing zeroes in the end. e.g. 10.00, -4.567
  • char – Can store single characters only which are surrounded by single quotes e.g. ‘a’, ‘B’.
  • string – Can store text values and are surrounded by double quotes e.g. “This is a text”.
  • bool – Can only store values with two different states which are either TRUE or FALSE.

How to declare a variable

Before declaring our variable, we should familiarize ourselves with some rules of variable declaration which are as follows:

  • A variable name can contain digits, letters, and underscore and must begin with a letter or an underscore. It cannot begin with a digit or special character other than underscore.
  • Variables names are case sensitive and hence Var and var are not the same variables.
  • Variable names cannot contain any whitespaces or special characters except underscore in them.
  • Variable names cannot be a reserved keyword of C++.

Below are the examples of some of the valid variable names:

Var, My_Num , _var , variable42

Some examples of Invalid Variable names :

34variable , My@Num , email address , int

You can declare one or multiple variables in a single line.

int x; // Single variable Declaration
int x,y,z; // Multiple variable Declarations

You can also declare and initialize one or multiple variables in the same line:

int x=5; // Single variable Declaration and Initialization
int x=1,y=2,z=3; // Multiple variable Declarations and Initializations
int a=1,b,c; //This will throw an error

Constant Variables

A constant variable is one where your need to declare the value of the variable during declaration and that value can only be accessed later by the program and cannot be changed. It will throw an error if the program tries to change the value of a constant. In C++ you can declare a constant variable using the keyword const.

Eg: const float PI =3.14;

Trying to change the value of the variable PI in the code will result in an error as it has been declared as a constant.

Lvalue and Rvalue

There are two different types of expression types in C++ which are ‘lvalue’ and ‘rvalue’.

1) Lvalue

Any expression which directly refers to a memory location which is usually a variable or a pointer is known as an ‘lvalue’. They can appear on both left and right-hand sides of an assignment if the syntax is correct.

int g; //g is an lvalue
g=10;
int k=g; //k is an lvalue and the lvalue 'g' is on the right-hand side.

2) Rvalue

Any expression where the data stored at a memory location is accessed is known as an rvalue. An rvalue is always an expression that cannot have any value assigned to it and it can only be assigned to another lvalue by keeping it on the right. It always appears on the right side of an expression.

int x=10; // Proper rvalue assignment
int 5=20; // Wrong rvalue assignment. It will throw an error.

Please get connected & share!

Advertisement