Is there any need of “long” data type in C and C++?

2

In C and C++, there are four different data type available for holding the integers i.e., short, int, long and long long. Each of these data type requires different amounts of memory.
But there is a catch, the size of “long” data type is not fixed unlike other data types. It varies from architectures, operating system and even with compiler that we are using. In some of the systems it behaves like an int data type or a long long data type as follows:

  OS               Architecture          Size
Windows       IA-32                     4 bytes
Windows       Intel® 64 or IA-64        4 bytes
Linux         IA-32                     4 bytes
Linux         Intel® 64 or IA-64        8 bytes
Mac OS X      IA-32                     4 bytes
Mac OS X      Intel® 64 or IA-64        8 bytes 

Well it also varies from compiler. But before this, let’s understand about the concept of cross compiler.
A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running.
For instance, if I compile the following programs in 64 bit architecture running a 64 bit Ubuntu, I will get the result like this:

// C program to check the size of 'long'
//  data type
#include<stdio.h>
int main()
{
    printf("Size of int = %ld\n", sizeof(int));
    printf("Size of long = %ld\n", sizeof(long));
    printf("Size of long long = %ld", sizeof(long long));
}
Output in 32 bit gcc compiler:-
Size of int = 4
Size of long = 4
Size of long long = 8

Output in 64 bit gcc compiler:-
Size of int = 4
Size of long = 8
Size of long long = 8

See this article to know more about how to compile a program with 32-bit or 64-bit gcc compiler.

From above we conclude that size of only “long” data type varies from compiler. Now the question is what exactly is happening here? Let’s discuss it in the way of how compiler allocates memory internally.

CPU calls data from RAM by giving the address of the location to MAR (Memory Address Register). The location is found and the data is transferred to MDR (Memory Data Register). This data is recorded in one of the Registers in the Processor for further processing. That’s why size of Data Bus determines the size of Registers in Processor. Now, a 32 bit register can call data of 4 bytes size only, at a time. And if the data size exceeds 32 bits, then it would required two cycles of fetching to have the data in it. This slows down the speed of 32 bit Machine compared to 64 bit, which would complete the operation in ONE fetch cycle only. So, obviously for the smaller data, it makes no difference if my processors are clocked at the same speed. Compilers are designed to generate the most efficient code for the target machine architecture.

So, in short the size of a variable is compiler dependent as it generates the instructions based on the target architecture and system architecture that only deals with the size of data bus and it’s transfer.
Note: Interestingly we don’t have any need of “long” data type as their replacement(int, long long) is already available from C99 standard.

Suggestion: If it is important to you for integer types to have the same size on all Intel platforms, then consider replacing “long” by either “int” or “long long”. The size of the “int” integer type is 4 bytes and the size of the “long long” integer type is 8 bytes for all the above combinations of operating system, architecture and compiler.

References:
https://software.intel.com/en-us/articles/size-of-long-integer-type-on-different-architecture-and-os

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.