Why does int = long in C?

In C, the integer datatypes are shortint, long, and long long. Now technically, each of these should contain a certain range of (signed) numbers, for example (according to the C99 standard):

short:     -32767 to 32767
int:       -32767 to 32767
long:      -2147483647 to 2147483647
long long: -9223372036854775807 to 9223372036854775807

This would make sense, but things aren’t always that simple. It always depends on the system. Here are the specs for a 64-bit OSX system:

size of short     : 2 bytes
size of int       : 4 bytes
size of long      : 8 bytes
size of long long : 8 bytes
range of short     : -32768 to 32767
range of int       : -2147483648 to 2147483647
range of long      : -9223372036854775808 to 9223372036854775807
range of long long : -9223372036854775808 to 9223372036854775807

So long and long long are the same on this system.

On a Raspberry Pi (with a 32-bit ARM processor) however, a different story emerges:

size of short     : 2
size of int       : 4
size of long      : 4
size of long long : 8
range of short     : -32768 to 32767
range of int       : -2147483648 to 2147483647
range of long      : -2147483648 to 2147483647
range of long long : -9223372036854775808 to 9223372036854775807

Here, int and long are the same.

The moral of the story? CHECK the limits of your datatypes before you use them if you plan to process large numbers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s