your image

Is there any need of "long" data type in C and C++? - GeeksforGeeks

greeksforgeeks
Related Topic
:- C language C++ language programming languages

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

  • Difficulty Level : Easy
  • Last Updated : 30 Mar, 2020

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          SizeWindows       IA-32                     4 bytesWindows       Intel® 64 or IA-64        4 bytesLinux         IA-32                     4 bytesLinux         Intel® 64 or IA-64        8 bytesMac OS X      IA-32                     4 bytesMac 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++
  • C

 

 

 

// 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

Output in 32 bit gcc compiler:-Size of int = 4Size of long = 4Size of long long = 8Output in 64 bit gcc compiler:-Size of int = 4Size of long = 8Size 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.

Comments