## Introduction to C++ long

In C++, long is a data type for a constant or variable which has the capability of storing the variable or constant values with 64-bits storage and is signed integer data type which is used for storing variable or constants with larger values larger than standard integer 32-bit data type. In general, long is a basic fundamental data type that is implemented by all programming languages for storing variable or constant values as larger than number 2,147,483,647 (231 ÷ 2) and even the small number with a limited number as a 32-bit value that is supported by almost all the systems which fall into the numerical integer type.

### Working of long Data Type in C++

In this article, we will discuss the long data type in C++. The long data type is a basic numerical signed and unsigned integer type which is used for specifying the storage size and location of variables or constants that are used in programs that can hold values as long as a single 64-bit signed (numbers can be either positive or negative) integer type or unsigned (only positive numbers) integer type that is long can store 263 with one bit for the sign. This long type is used to store large values that are equivalent to the long int type. These long type variables can be used with double variables also as it can be used as a long int. In C++ there is also another data type where it can help us store numbers larger than long and such variables can be stored with data type long long which is created twice using long data type but this long long type modifier can only be used with the int data type. In C++, there are specific literals provided to these data types such as for long type we use either “L” or “l” is used to identify long literal by suffixing these alphabets and for long long int we suffix with “LL” literal which are used for signed and if we want to indicate unsigned long then suffix wit “UL” and unsigned long long int then we have to suffix with “ULL”.

### Examples of C++ long

Now in the below section let us see how to declare long variables in a program.

#### Example #1

**Code:**

```
#include<iostream>
using namespace std;
int main()
{
long a = 4564667;
long int b = 4564667;
cout <<"Program to demonstrate long in C++" <<"\n"<<endl;
cout <<"The value of variable with long variable a is "<< a <<"\n"<< endl;
cout <<"The value of variable with log int variable b is "<< b << "\n"<<endl;
cout <<"The size of long variable a is " << sizeof(a) <<"\n" << endl;
cout <<"The size of long int variable b is "<< sizeof(b) <<"\n" <<endl;
return 0;
}
```

**Output:**

In the above program, we can see we have created a variable with long type “a” and another variable “b” with long int type these both have the same value so we can say long long and long int type is the same and we also can see we are declaring the size of the variables which can be done using sizeof() function. So we can say the size of long and long int have the same size that is 8.

#### Example #2

Now we will see the example of long and long types in the below program:

**Code:**

```
#include <iostream>
using namespace std;
int main ()
{
long p, q;
long long res;
p = 5456943859;
q = 254689;
res = p * q;
cout<<"Program to demonstrate long long type in C++" <<"\n"<<endl;
cout<<"The long type varaibles p " << p << " and q " <<q << "is \n"<<endl;
cout <<"The product of two long variables p*q = "<< res <<endl;;
cout<< sizeof(res)<<endl;
return 0;
}
```

**Output:**

In the above program, we can see we have declared two long variables p and q and we are trying to find the product of these two variables which will be a very large value so we are declaring the variable “res” which will be the long long type which can store large value than long type. So in the above screenshot, we can see the output, and also we can see a size of long long variable “res” which prints 8 and the product value is 1389823574504851.

#### Example #3

Now we will see a sample example of using literals for long unsigned and signed data types.

**Code:**

```
#include <iostream>
using namespace std;
int main ()
{
long p = -5456943859L;
long z = 8647627343UL;
long long r = 1389823574504851LL;
cout<<"Program to demonstrate long long and long type literals in C++" <<"\n"<<endl;
cout<<"The long type varaibles p literal is " << p <<"\n"<<endl;
cout<<"The long type variable z literal of unsigned long variable is "<<z <<"\n"<<endl;
cout<<"The long long type of variable r literal is = "<< r <<endl;
return 0;
}
```

**Output:**

In the above program, we can see we have declared a long variable “p” with value and we have suffixed it with “L” and we have also declared long long type variable “r” and we have suffixed it with “LL”. In the above program, we have seen we have p value suffixed with L as “5456943859L” and r variable which is the long long type with value 1389823574504851LL where we can see p and r are signed long variables. So the variable “z” is also a long type but it is unsigned so it can have only a positive number which can be seen in the above screenshot.

### Conclusion

In this article, we can conclude that the long type variable in C++ is a type of data type which is used for storing the values that are as large as 64-bit values, and in C++ it also provides a data type long long which is another data type to store values larger than the long type which means it can hold the value twice of long type. In this article, we have seen a simple example of how to declare the long type variables which is equivalent to long int in C++. In this article, we also saw the example of long long type variables along with the literals which are used by suffixing it with value to indicate the signed or unsigned long type variables.

### Recommended Articles

This is a guide to C++ long. Here we also discuss the introduction and working of long data type in c++ along with different examples and its code implementation. You may also have a look at the following articles to learn more –

4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses