Integer Variable, Long Integer and Short Integer in C++

☯ All C++ Articles☯ All Articles

A new product.

Last updated on: .

Integer Variables:
Let a simple program:
#include <iostream>
using namespace std;
int bird = 6;
int sky = 3;
int tree;
int main()
{
tree = bird + sky;
cout << "The total is:" << tree << endl;
return 0;
}

Integer variables are variables that have only values of whole numbers. Such as the number of doors in a house. This number can not be fraction.

On the above program
int bird = 6;
the variable bird is an Integer Variable and it has a value 6. The integer variable is declared with the keyword int.

◕ This integer variable can also have a negative value like
int bird = - 6;

◕ But this integer variable can't have a value like the following
int bird = 6.52;
Because this is not a whole number. This is a floating number and has another keyword float.

Generally the integer variable occupies 4 bytes of the memory depending upon the computer system. but we can increase and decrease this limit by using other keyword short or long. Let have example.

Short Integer Variables

short bird = 6;

The keyword short also defines as an Integer Variable. Now the integer variable will occupies only 2 bytes of the computer's memory, not 4 bytes.

Long Integer Variables

long bird = 6L;

The keyword long also defines as an Integer Variable. Now the integer variable will occupie 8 bytes of the computer's memory, not 4 bytes.

This 8 bytes can store values from - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

In the following statement
long bird = 6L;

there is a L at the end of the statement. This L indicates that this integer is a long integer. We can also use small letter as l but it confuses with the digit 1.

Another point is that, an integer value without L is consider as the normal integer value, not the long integer value.