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 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 of long int
long int var1;
In C, we can skip the int keyword and just declare the variable with only long and it will be equivalent to long int declaration.
long var2;
Here, both var1 and var2 are of the same datatype and carry the same size.
Example of long int
C
// C program to illustrate long int #include <stdio.h> int main() { // declaring some int and long int variables int ni; long int li1; long li2; // assigning some big number to the variables ni = li1 = li2 = 3000000000; // printing valuees printf ( "Values:\n\tint = %d\n\tlong int1 = %ld\n\tlong " "int2 = %ld\n" , ni, li1, li2); printf ( "Size:\n\tint = %d\n\tlong int1 = %ld\n\tlong " "int2 = %ld\n" , sizeof (ni), sizeof (li1), sizeof (li2)); return 0; } |
Output
Values: int = -1294967296 long int1 = 3000000000 long int2 = 3000000000 Size: int = 2 long int1 = 4 long int2 = 4
As we can see, the long int variables can store larger numbers than int variables and also have a greater size than int.
Note: The size of the long int and other variables varies depending upon the compiler and system architecture.
C Long
In C, the long keyword is a datatype modifier that can be applied to the basic datatypes to increase the range and size of values their variables can store. For example, a long int variable can store integer values that range from -2,147,483,648 to 2,147,483,647 which is significantly greater as compared to the normal int data type, which can store integer values that range from -32,768 to 32,767 in the 16-bit compiler.
Contact Us