In the early days of C++ there were few data types such as char, float, and int. Over time these types improved with new additions. Modern C++ is really amazing, it allows you to define your data type in accordance with the limits of your variable values. One of the largest integer types is the unsigned long long
or unsigned long long int
and in this post we explain how to use the long long int
type.
Table of Contents
What are the fundamental variables In C++?
A professional developer should always know the size of data types and their limits and which data types of a variable is needed in these limits, because the operating system allocates memory and decides what can be stored in the reserved memory.
In addition to standard types in C++, there are fixed-width integers which are defined types in header <cstdint>
with a fixed number of bits. Fixed-width integers are called as intN_t
or intX_t
integers where N or X represents number of bits reserved for that type.
What is long long int type in C++?
The long long integral type (or long long int) is an integer type for the larger numbers that doubles bytes of long type integer. The long long
and long long int
types are identical as same as long
and long int
types are identical. In modern C++, C++11 standard introduced long long integral type to be more compatible with C99 standards. Detailed information on this feature, can be seen in this proposal: long long type Proposal document.
Adding long long was proposed previously by Roland Hartinger in June of 1995. At the time, long long had not been considered by the C committee, and the C++ committee was reluctant to add a fundamental type that was not also in C. Almost a decade later long long was part of C99, and many major C++ compilers start to support it. long long has been added to standards with C++11.
If we consider that int
variable has size of 4 bytes, long long int has size of 8 bytes. usigned long long int is same.
Here are the most used integer types in C++,
Type | Typical Bit Width | Typical Range |
---|---|---|
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32768 to 32767 |
long int | 4bytes | -2,147,483,648 to 2,147,483,647 |
signed long int | 4bytes | same as long int |
unsigned long int | 4bytes | 0 to 4,294,967,295 |
long long int | 8bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
Is there a simple example of how to use the long long int type in C++?
Here is a simple example that shows how you can use int, long int long long int, unsigned long long int
,
1 2 3 4 5 6 |
int i = 2147483647; long int l = 2147483647; long long int ll = 9223372036854775807; unsigned long long int ull= 18446744073709551615; |
Is there an example of how to use long long int type in C++?
Here is an example of how you can use long long
, long long int
and unsigned long long
, unsigned long long int
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> int main() { int i = 2147483647; long int l = 2147483647; long long int ll = 9223372036854775807; unsigned long long int ull= 18446744073709551615; std::cout << "i=" << i << " " << sizeof(i) << "bytes" << std::endl; std::cout << "l=" << l << " " << sizeof(l) << "bytes" << std::endl; std::cout << "ll=" << ll << " " << sizeof(ll) << "bytes" << std::endl; std::cout << "ull=" << ull << " " << sizeof(ull) << "bytes" << std::endl; system("pause"); return 0; } |
here is the output,
1 2 3 4 5 6 7 |
i=2147483647 4bytes l=2147483647 4bytes ll=9223372036854775807 8bytes ull=18446744073709551615 8bytes Press any key to continue . . . |
Are there larger int types than the long long int type in C++?
One of the most common scientific problems in programming is the limitation of numbers that we use. If you are about to calculate very large numbers (i.e. factorial 100!) there are the Boost C++ Libraries which is a set of C++ libraries that you can use to help with computational operations. Do you know that you can use very large numbers with Boost libraries with almost any modern C++ compiler? Here is how you can use very large integer numbers,
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.