C Long (original) (raw)
Last Updated : 15 Jun, 2026
The long keyword in C is a data type modifier used to increase the storage capacity and range of numeric data types. It helps store larger values and reduces the risk of overflow.
- Extends the range of standard types like int to handle bigger numbers.
- Can be used as long int, long long int, or long double based on precision needs.
- Provides greater capacity but typically uses more memory than standard data types.
**Example: A standard int may store values up to around 32,767 on some systems, while a long int can store values exceeding 2 billion.
Using the long Modifier in C
The long modifier is added before a valid data type during variable declaration to increase its storage capacity and numerical range.
- Add long as a prefix while declaring a variable, such as long int var;.
- It can only be used with int, long long int, and long double in C.
- The size of long types may vary depending on the compiler and system architecture.
**Example:
int var; // Normal declaration
long int var; // Long integer declaration
long int is used when a larger range is required than a standard int can provide.
1. long int
When long is applied to an integer it creates a long int, which increases the range of values that int can store. The long int is used when the range of values exceeds the int data type.
- The size of the long int is typically 4 bytes.
- It can store integer values that range from -2,147,483,648 to 2,147,483,647.
- The format specifier for long int is %ld.
- The long int literals can be represented by a numeric value followed by "L" or "l" as the suffix.
Syntax
long int var1; or, long var2;
Here, both var1 and var2 are of the same datatype and carry the same size.
**Example: The following code compares the storage capacity and memory size of a standard int versus a long int.
C `
#include <stdio.h>
int main() { // Max 32-bit int int a = 2147483647;
// Exceeds standard 32-bit int
long b = 3000000000L;
printf("Long Value: %ld\n", b);
printf("Sizes: int = %zu, long = %zu bytes\n",
sizeof(a), sizeof(b));
return 0;}
`
Output
Long Value: 3000000000 Sizes: int = 4, long = 8 bytes
2. long long int
We can even apply the long modifier to long int to further increase the size of the data type. The long long int is often used when the range of values exceeds the long data type.
- The size of the long long int is typically 8 bytes.
- It can represent integer values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- The format specifier for the long long data type is %lld.
- The long int literals can be represented by a numeric value followed by "LL" or "ll" as the suffix.
Syntax
long long int var; or, long long var;
**Example: This example demonstrates how to declare long long variables and check their memory footprint using sizeof.
C `
#include <stdio.h>
int main() { long long var = 9223372036854775807LL; printf("Large Value: %lld\n", var); printf("Size of long long: %zu bytes\n", sizeof(var)); return 0; }
`
Output
Large Value: 9223372036854775807 Size of long long: 8 bytes
3. long double
We can use a long modifier with double datatype to create a long double variable. Long double is used to store significantly large decimal numbers which surpass the range of double.
- The size of a long double is typically 16 bytes.
- It provides more accuracy, precision and a larger range than the standard double data type.
- The format specifier for long double is %Lf.
Syntax
long double var;
**Example: In below example, we initialize a long double to observe its increased precision and size.
C `
#include <stdio.h>
int main() { long double high_prec = 3.141592653589793238L; printf("Long Double: %Lf\n", high_prec); printf("Size: %zu bytes\n", sizeof(high_prec)); return 0; }
`
Output
Long Double: 3.141593 Size: 16 bytes
Use Cases of long in C
The long modifier should be used when the range or precision provided by standard data types is not sufficient for your application.
- Use it when the data you are handling is expected to exceed the maximum limit of standard int or double.
- Use long long if you specifically need a 64-bit integer, as the size of long can vary between Windows and Linux.
- Use long double in scientific or financial applications where decimal accuracy is critical.
- Avoid using long modifiers by default - they consume more RAM and can slightly slow down processing on some architectures. Only use them when the range is necessary.