Long int in C
- We use data type to avoid the type of data to be stored for a variable at the time of declaration.
- So, we can say that the variable type is known by the data type assigned to it and what type of data the variable can store.
- As we know, there are different data types, so the memory allocation differs from one another. The memory is created by the compiler when any of the variables is declared.
- The memory allocation depends on the data type we declared for a variable. The size also depends on the system on which it is operating. Which takes 4 – bytes or sometimes it takes 2 – bytes.
- Let us consider a list of ranges for memory requirements for a 32 – bit compiler
Sl no Data Type Bytes Range
1 int 4 - 2 ^ 31 to 2 ^ 31 – 1
2 Long int 4 - 2 ^ 31 to 2 ^ 31 – 1
3 Long Long int 8 - 2 ^ 63 to 2 ^ 63 - 1
The order of size is:
Char <= short <= int <= long <= long long
- The size of the long int is between -2147483648 to 2147483647. This means the size of long int cannot go beyond this limit.
- Let us consider a program for this scenario.
/ / program in to implement
/ / the above approach
# include <stdio.h>
/ / main function
int main ( ) {
/ / declaring the variables
int a ,b , c ;
a = 99999;
b = 99999;
/ / performing the multiplication operation
c = a * b;
/ / printing the result
printf(“ The result is %d “, c );
return 0;
}
Output:
The result is 1539894908
- As you can see, that output is not correct because it cannot store values out of range 10 ^ 10. So we have use increase the range to long int.
Let us consider a program where we use a long variable to print the result:
/ / program in c to implement the above scenario
# include <stdio.h>
/ / main function
int main ( ) {
/ / declaring int variables
int a, b;
/ / declaring long variable
long c;
a = 99999;
b = 99999;
/ / performing the multiplication operation
c = a * b;
/ / print the result
printf(“ the result is %ld “ ,c );
return 0;
}
Output:
The result is 1539894908
- We can see the output is still the same even after converting to long because before assigning the result of multiplication of a and b, it is overflowed.
- So must to convert int results to long before assigning the result value.
Let us consider a program to implement the above scenario:
/ / c program to implement
/ / above approach
# include<stdio.h>
/ / main function
int main ( ) {
/ / declare the variables
long a, b, c;
a = 99999;
b = 99999;
/ / performing the operation
c = a * b ;
/ / printing the result
printf(“ the result is %ld” , c);
return 0;
}
Output:
The result is 9999800001
- In the above case, the output printed is correct because the range is sufficient for long to store.
- In a long int data type, its size is 4.
- The sizeof( ) function is used to find the size of the data type.
- Let us consider a program to find the size of long int data type using sizeof( ) operator.
/ / c program to implement
/ / above case
# include <stdio.h>
/ / main function
Int main ( ) {
/ / declaring int variable
Int a;
/ / declaring long variable
Long b;
Printf(“ enter a value for a: \n”);
Scanf (“%d”, &a);
Printf(“ enter a value for b :\n”);
Scanf(“%ld” , &b);
/ / displaying the size of datatype
Printf(“ the size of %d datatype is %lu byte \n”,a,sizeof( int) );
Printf(“ the size of %d datatype is %lu byte \n”, b ,sizeof( long) );
return 0;
}
Output:
Enter a value for a: 2
Enter a value for b:99
The size of a datatype is 4 byte
The size of b datatype is 4 byte