What is variables in C++

Variables in C++ are the name of a memory address to store one type of data. A variable has a specific type in C++ by which it refers the type of data it can hold. For this we can said the variable as a container which can contain data inside it.

Some most common types of variables in C++ which we use are as follows;

  • char — char variable can contain a character inside it like ‘a’, ‘b’, ‘X’, ‘Y’ etc. Value of char variable must have to write inside a single quotation mark.
  • int — int variable can contains any integer inside it (It has a limit of holding maximum or minimum integer) like 7, 12, 45, 720 etc.
  • float — float can hold a single precision floating point number like 2.34, 4.82, 23.435 etc.
  • double — double holds double precision floating point number. We use double to hold floating point number of large size.
  • bool — bool can only holds two value of either true or false.

There are some other variables too which we will learn step by step in our future articles. We will cover them in array, pointers, enum, data structures, classes, reference etc.

Declaration of variables in C++

Let’s see some example of declaring a variable.

char x;    // can store a character

int y;    // can store an integer

float myHeight;    // can store a floating point number

However, we can assign a value at the time of declaration of a variable as well as later. Here, we have assigned the value at the time of of declaring a variable in first block where we have assigned value later in the second block.

// assigning value at the time of declaring variables in c++

char x = 'p';

int y = 30;

float myHeight = 5.11;
// assigning value after declaring a variable

char x;
int y;
float myHeight;

x = 'p';
y = 30;
myHeight = 5.11;

Although we can give different name of a variable but it is recommended to give a meaningful name of any variable. This will help to understand the program later and increase the readability of the program. We should named variable like myHeight, firstNum, my_height, first_num etc instead of x, y, z, a, b c.

Rules for naming variables in C++

There are some rules for naming a variable. These are almost same for any programming language. Let’s see the rules of naming a variable.

  • Variables can only contain uppercase and lowercase letter, digits and underscore.
  • First letter must have to a letter or underscore not any digit.
  • Using keywords as a variable name is not valid. It will through an error.
  • Spaces are not allowed inside the name of a variable.
  • Variable name will always exist on the left side of assignment operators.
char float;    // invalid! float is a keyword
char ch;       // valid

int 4th_num;     // invalid! digit at the beginning of name
int fourth_num    // valid

float my height;    // invalid! space is not valid inside name
float my_height;    // valid
float myHeight;     // valid

Multiple variable declaration in C++

It is valid to declare multiple variable in single line in C++. We can also assign value inside that single line or later.

// multiple variable declaration
int a, b, num, myWeight;

// declaration with assigning value
int x = 10, y = 12, num = 5, myWeight = 72;

Previous pageComments in C++

Next page:  Variables scope