Need of long data type in C


In C or C++, there are four different datatypes, that are used for integer type data. These four datatypes are short, int, long and long long. Each of these datatypes takes different memory spaces. The size varies in different architecture and different operating systems. Sometimes int takes 4-bytes or sometimes it takes 2-bytes. This also happen for the compilers. So we can use cross compilers.

The cross compilers are basically a compiler, which is capable of compiling for a platform other than current platform.

So if we want to compile the following code in 32bit system, and 64-bit system, it will generate different outputs.

Example

#include<stdio.h>
int main() {
   printf("Size of int : %ld Bytes
", sizeof(int));    printf("Size of long : %ld Bytes
", sizeof(long));    printf("Size of long long : %ld Bytes", sizeof(long long)); }

Output

Size of int : 4 Bytes
Size of long : 4 Bytes
Size of long long : 8 Bytes

So from this example we can easily understand that the long datatype varies from compiler. So what is the reason behind it?

The CPU calls data from primary memory (RAM) by providing the address of Memory Address Register (MAR). When the location is found, it is transferred to Memory Buffer Register (MBR). The data is stored into the CPU register for further usage. So the size of data bus determines the size of CPU register. For 32-bit system, it can call only 4-byte data at a time. If data is larger than 32bit, then it will take two cycles. So for smaller data it does not make any difference.

Updated on: 30-Jul-2019

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements