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:
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.
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 firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- What is data type of FILE in C ?
- Bool data type in C++
- What is the size_t data type in C?
- C++ map having key as a user define data type
- Data Type Ranges and their macros in C++
- C++ set for user define data type
- Data type of character constants in C and C++
- Multi-set for user defined data type
- Comparison of boolean data type in C++ and Java
- Data type of case labels of switch statement in C++?
- Conversion of Struct data type to Hex String and vice versa
- Type Conversion in C++
- Type of 'this' pointer in C++
- Type Conversion in C
- Array Type Manipulation in C++