Open In App
Related Articles

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

Like Article
Save Article
Report issue
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 <bits/stdc++.h>
using namespace std;
int main() 
    cout << "Size of int = "<< sizeof(int) << endl; 
    cout << "Size of long = " << sizeof(long) << endl; 
    cout << "Size of long long = " << sizeof(long long); 
// This code is contributed by shubhamsingh10



// C program to check the size of 'long'
//  data type
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.

Last Updated : 30 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads