Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is long long in C/C++?
In C programming, long long is an extended integer data type that provides a larger range for storing integer values than the standard long type. The long long type was introduced in C99 standard to handle very large integer values that exceed the capacity of regular int or long types.
Syntax
long long variable_name; long long int variable_name; // equivalent to above
Size and Range
The long long data type is guaranteed to be at least 64 bits (8 bytes) according to the C standard. The exact size may vary between systems, but it's typically twice the size of long on many platforms.
Example: Checking Sizes of Integer Types
Let's examine the memory allocation for different integer types −
#include <stdio.h>
int main() {
int a;
long b;
long long c;
printf("Size of int = %lu bytes\n", sizeof(a));
printf("Size of long = %lu bytes\n", sizeof(b));
printf("Size of long long = %lu bytes\n", sizeof(c));
return 0;
}
Size of int = 4 bytes Size of long = 4 bytes Size of long long = 8 bytes
Example: Working with Large Numbers
Here's how to declare and use long long variables for large integer values −
#include <stdio.h>
int main() {
long long large_number = 9223372036854775807LL; // Maximum value
long long factorial = 1LL;
printf("Large number: %lld\n", large_number);
/* Calculate factorial of 20 */
for(int i = 1; i <= 20; i++) {
factorial *= i;
}
printf("Factorial of 20: %lld\n", factorial);
return 0;
}
Large number: 9223372036854775807 Factorial of 20: 2432902008176640000
Key Points
- Use
%lldformat specifier withprintf()andscanf()forlong longvalues. - Append
LLsuffix to large integer literals to ensure they are treated aslong long. - The range is typically from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 for signed
long long. - Output may vary on different systems due to different architectures.
Conclusion
The long long data type in C is essential for handling large integer values that exceed the capacity of standard integer types. It provides at least 64-bit storage and is particularly useful in mathematical computations involving large numbers.
