C++ long | Working of Long Data Type in C++ with Examples (2023)

C++ long | Working of Long Data Type in C++ with Examples (1)

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”.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Examples of C++ long

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

Example #1


#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;}


C++ long | Working of Long Data Type in C++ with Examples (2)

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:


#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;}


C++ long | Working of Long Data Type in C++ with Examples (3)

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.


#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;}


C++ long | Working of Long Data Type in C++ with Examples (4)

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.


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 –

  1. C++ Max
  2. C++ reserve()
  3. C++ static_cast
  4. C++ sort()
JENKINS Certification Course 19+ Hours of HD Videos 6 Courses Verifiable Certificate of Completion Lifetime Access4.5
Primary Sidebar

Popular Course in this category

Price View Course


What is an example of long data type in C++? ›

long Type Modifier

If we need to store a large integer (in the range -2147483647 to 2147483647), we can use the type specifier long . For example, // large integer long b = 123456; Note: long is equivalent to long int .

What is the data type for long long int in C++? ›

Primitive Built-in Types
TypeTypical Bit WidthTypical Range
long int8bytes-9223372036854775808 to 9223372036854775807
signed long int8bytessame as long int
unsigned long int8bytes0 to 18446744073709551615
long long int8bytes-(2^63) to (2^63)-1
14 more rows

What is long long type in C++? ›

Longer integers: long

The long data type stores integers like int , but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long for a range of 0 to 4,294,967,295.

What is the data type of long long? ›

LongLong (LongLong integer) variables are stored as signed 64-bit (8-byte) numbers ranging in value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The type-declaration character for LongLong is the caret (^). LongLong is a valid declared type only on 64-bit platforms.

What is an example of long data type? ›

Long data types are whole numbers, both positive and negative, that have many place values. Examples include: -398,741,129,664,271.

How many digits is long long C++? ›

In this article
Type NameBytesRange of Values
unsigned long40 to 4,294,967,295
long long8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long80 to 18,446,744,073,709,551,615
21 more rows
Aug 2, 2021

How do you define long long? ›

define ll long long is used so that every int in the whole function becomes long long and we don't have to write it again and again.

How do you use long long int? ›

A long int typically uses twice as many bits as a regular int, allowing it to hold much larger numbers. printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int. long int may also be specified as just long.

Is long the same as int in C++? ›

"a long in C/C++ is the same length as an int." Not always. The C++ standard specifies that an int be the "natural" size for the processor, which may not always be as big as a long . The standard also guarantees that a long is at least as long as an int , so the fact that they are equal sizes are not always guaranteed.

How many bits is long long? ›

The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits.

What is the difference between long long and int? ›

int is 32 bits. long is 32 bits as well. long long is 64 bits.

Where do we use long data types? ›

The long data type is used when you need a range of values more than those provided by int. Example: long a = 100000L, long b = -200000L.

What is long text data type? ›

A Long Text field can be useful for storing large amounts of information, such as notes, comments, and descriptions. The Long Text data type stores up to 65,536 alphanumeric characters and supports rich text formatting, such as different colors, fonts, and highlighting.

What is long vs real data type? ›

LReal is a double precision real, float, or floating point variables that is a 64 bit signed value rather then a real is a single precision real, float, or floating point that is made from a 32 bit signed value. So it stores more in a LReal which makes LReal closer to a Double and a Float.

What is the smallest long long C++? ›

The C++ standard only guarantees that the minimum size for long long int will be 64-bits. This is also by far the most common size.

What is the size of an long? ›

Data Types and Sizes
Type Name32–bit Size64–bit Size
short2 bytes2 bytes
int4 bytes4 bytes
long4 bytes8 bytes
long long8 bytes8 bytes
1 more row

Why use int instead of long? ›

Integer is a signed 32 bit integer type. If your usage of a variable falls in the 32 bit range, use `Int`, else use `long`. Usually long is used for scientific computations and stuff like that need much accuracy.

Is long the same as long long? ›

But before starting the blog post, I want to make you clear that long and long int are identical and also long long and long long int. In both cases, the int is optional. There are several shorthands for built-in types. Let's see some examples of signed built-in types.

What is a long integer example? ›

Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647. The type-declaration character for Long is the ampersand (&).

What is the max limit of long long int? ›


How many digits is a long int? ›

long int : -2,147,483,647 to 2,147,483,647. unsigned long int : 0 to 4,294,967,295.

Is Long Long always 64 bits? ›

long , ptr , and off_t are all 64 bits (8 bytes) in size.

Is 64-bit long or long long? ›

long long - target type will have width of at least 64 bits. Note: as with all type specifiers, any order is permitted: unsigned long long int and long int unsigned long name the same type.

Is long long always 8 bytes? ›

long and long long are both 8 bytes.

Is long an int or double? ›

Long is for integer numbers. Double is for real numbers (i.e. numbers which have decimal points in them!). (They can have up to around 12 significant figures.)

Can I use int instead of long? ›

Traditionally, the "int" type is the "natural" type for the processor - i.e., it's the size of the processor's registers. This can be 4, 8, 16, 32... bits, but usually the processor can handle it as fast as possible. Type "long" is for when you need more precision, even at the expense of slower code.

Is there long string in C++? ›

Long strings can be written in multiple lines by using two double quotes ( “ “ ) to break the string at any point in the middle.

Is a long a decimal C++? ›

There are three data types that can store decimals : float, double and long double.

What is the function to convert long long to string in C++? ›

lltoa() — Convert long long into a string

Compile requirement: Use of this function requires the long long data type. See z/OS XL C/C++ Language Reference for information on how to make long long available.

What is the Len function in C++? ›

C++ String length()

This function is used to find the length of the string in terms of bytes. This is the actual number of bytes that conform the contents of the string , which is not necessarily equal to the storage capacity.

What is a C++ long double? ›

The long double is another data type in C++ that is used to store floating-point numbers. It takes up the size of up to 12 bytes whereas the float and the double occupy 4 bytes and 8 bytes respectively. The values initialized with the data type long double must end with “L”.


Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated: 01/12/2023

Views: 6386

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.