Open In App

Working of Keyword long in C programming

In C, long is a keyword that symbolizes the Long datatype. The long data type is a 64-bit two’s complement integer with:

Size: 64 bit
Value: -263 to 263-1

The output of 64 bit GCC compiler would give the size of long as 8 whereas on 32 bit GCC compiler the size would be 4. So it varies from compiler to compiler. Now the question is what exactly is happening here? Let’s discuss the way how the compiler allocates memory internally. 

Compilers are designed to generate the most efficient code for the target machine architecture. 

Note:  Interestingly we don’t have any need for a “long” data type as their replacement(int, long long) is already available from the C99 standard. 

Data Type Storage Size Range Format Specifier
long 4 bytes -2,147,483,648 to +2,147,483,647 %ld
long long 8 bytes -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 %lld
long double 10 bytes 3.4E-4932 to 1.1E+4932 %lf

Example of a long keyword

In the below program, all the possible variables with long datatype are defined and their sizes are calculated and printed using sizeof() operator. 

Below is the C program to find the demonstrated working of long keywords: 




// C program to demonstrate the working
// of long keyword
#include <stdio.h>
 
// Driver code
int main()
{
    long longType;
    int integerType;
    long int longIntegerType;
    long long int longLongIntegerType;
    float floatType;
    double doubleType;
    long double longDoubleType;
 
    // Calculate and Print the size of all variables
    printf("Size of longType is: %ld\n", sizeof(longType));
 
    printf("Size of integerType is: %ld\n",
           sizeof(integerType));
 
    printf("Size of longIntegerType is: %ld\n",
           sizeof(longIntegerType));
 
    printf("Size of longLongIntegerType is: %ld\n",
           sizeof(longLongIntegerType));
 
    printf("Size of floatType is: %ld\n",
           sizeof(floatType));
 
    printf("Size of doubleType is: %ld\n",
           sizeof(doubleType));
 
    printf("Size of longDoubleType is: %ld\n",
           sizeof(longDoubleType));
 
    return 0;
}

Output
Size of longType is: 8
Size of integerType is: 4
Size of longIntegerType is: 8
Size of longLongIntegerType is: 8
Size of floatType is: 4
Size of doubleType is: 8
Size of longDoubleType is: 16

Article Tags :