1

I want to check the size, max value, and min value of data type int, long and their unsigned form. The output of my program shows that both int and long have the same size, max, and min value, same goes to their unsigned form. Here is the output of my program:

Size of int : 4 byte, Max value : 2147483647, Min value : -2147483648
Size of long : 4 byte, Max value : 2147483647, Min value : -2147483648

Size of unsigned int : 4 byte, Max value : 4294967295, Min value : 0
Size of unsigned long : 4 byte, Max value : 4294967295, Min value : 0

And here is my code:

#include <stdio.h>
#include <limits.h>

int main(int argc, char *argv[])
{
    printf("Size of int : %ld byte, Max value : %d, Min value : %d\n", sizeof(int), INT_MAX, INT_MIN);
    printf("Size of long : %ld byte, Max value : %d, Min value : %d\n", sizeof(long), LONG_MAX, LONG_MIN);
    printf("\n");
    printf("Size of unsigned int : %ld byte, Max value : %u, Min value : %d\n", sizeof(unsigned int), UINT_MAX, 0);
    printf("Size of unsigned long : %ld byte, Max value : %lu, Min value : %d\n", sizeof(unsigned long), ULONG_MAX, 0);
    return 0;
}

My question is, is this normal that int and long have the same size, max value, and min value? I am running the program using gcc version 5.1.0 (tdm64-1) compiler on Windows 10 64-bit machine.

9
  • Are you running your program in which platform, because I get different answer from the above code in Linux? Mar 16, 2018 at 12:41
  • See: c-faq.com/decl/inttypes.html
    – P.P
    Mar 16, 2018 at 12:43
  • Format specifier 7:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long int’ [-Wformat=] correct that to %ld. Mar 16, 2018 at 12:44
  • You have undefined behavior, you should use %zu to print size_t values (such as those from sizeof).
    – unwind
    Mar 16, 2018 at 12:45
  • I am running it on Windows @LethalProgrammer Mar 16, 2018 at 12:57

4 Answers 4

5

This relationship always hold:

short int <= int <= long int.

So in some cases int may have the same size as long, while in some other cases int may have the size as short. But int will never exceed long and will never fall below short. This is what the above statement(inequality) says.

In your case, int is equal to long.

1
  • 1
    I'd be inclined to say what the limit for short is here.
    – Bathsheba
    Mar 16, 2018 at 12:34
4

The C standard does permit int and long to have the same size and range. It's easiest to explain the rules for unsigned types, which have minimum maximums: each unsigned integer type must be able to represent 0 through at least some number. This is the table:

type                             minimum maximum
unsigned char                                255   (2**8  - 1)
unsigned short                            65,535   (2**16 - 1)
unsigned int                              65,535   (2**16 - 1)
unsigned long                      4,294,967,295   (2**32 - 1)
unsigned long long    18,446,744,073,709,551,615   (2**64 - 1)

So you can see that a configuration in which unsigned int and unsigned long have the same range is perfectly allowed, as long as that range is at least as big as the minimum range for unsigned long. The signed types are required to have the same overall value range as their unsigned counterparts, but shifted so that almost exactly half of the values are negative — unfortunately it's not as simple as "−2n−1 … 2n−1 − 1", because the standard continues to permit non-twos-complement implementations even though nobody has manufactured a CPU that does that in many years.

It's possible that you thought long would be able to represent up to 263 − 1 because that's true on most "64-bit" operating systems. But "64-bit" Windows is an exception. Almost all operating systems that call themselves "64-bit" use what is known as an "LP64 ABI", in which the integer types and void * have these sizes:

sizeof(short)     == 2
sizeof(int)       == 4
sizeof(long)      == 8
sizeof(long long) == 8
sizeof(void *)    == 8

Windows instead uses an "LLP64" ABI, with

sizeof(short)     == 2
sizeof(int)       == 4
sizeof(long)      == 4
sizeof(long long) == 8
sizeof(void *)    == 8

This is for backward compatibility with 32-bit Windows, in which long and int are also the same size; Microsoft thought too much existing code would break if they changed the size of long.

(Note that having sizeof(void*) > sizeof(long) is, for reasons too complicated to get into here, forbidden by the original 1989 C standard. Because they were determined to use LLP64 for 64-bit Windows, Microsoft rammed a change into C99 to permit it, over literally everyone else's explicit objections. And then, for over a decade after C99 came out, they didn't bother implementing the C99 features (e.g. uintptr_t and %zu) that were supposed to substitute for the relaxed requirement, leading to cumulative man-years of extra work for people trying to write programs that work on both Windows and not-Windows. Not That I'm Bitter™.)

1
  • There are other minimal constraints on range(short) other than it has to be no smaller than a char, for example.
    – Bathsheba
    Mar 16, 2018 at 19:34
2

Yes this is perfectly normal. An architecture with a 32 bit int, and 32 bit long, an a 64 bit pointer is called an LLP64 data model and is favoured by Windows (despite an Intel CPU itself using a 48 bit pointer internally).

(A 64 bit Linux architecture uses a LP64 data model which has a 32 bit int, a 64 bit long, and a 64 bit pointer.)

The C standard states that the minimum range for an int is -32767 to +32767, and a long -2147483647 to +2147483647. So both schemes are compliant with this.

2
  • How about on Linux machine? Would it be different? Mar 16, 2018 at 12:30
  • 3
    @AkhmadZaki: Yes it can be
    – Bathsheba
    Mar 16, 2018 at 12:31
0

From the C Standard (6.2.5 Types)

8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

So all that is required is that the range of values of the type int would be no greater than the range of values of the type long.

And (6.3.1.1 Boolean, characters, and integers)

1 Every integer type has an integer conversion rank defined as follows:

— The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

So though objects of the type int and long can have the same representation and correspondingly the same range of values nevertheless the rank of the type long is higher than the rank of the type int.

For example if the type int and long have the same representation the type of the expression x + y in the code snippet below will be long.

int x = 0;
long y = 0;

printf( "%ld\n", x + Y );

Not only the int and long types can have the same representation. On 64-bit systems for example the type long int and long long int also can coincide.

For example the output of this program running at www.ideone.com

#include <stdio.h>

int main(void) 
{
    printf( "sizeof( long int ) = %zu\n", sizeof( long int ) );
    printf( "sizeof( long long int ) = %zu\n", sizeof( long long int ) );

    return 0;
} 

is

sizeof( long int ) = 8
sizeof( long long int ) = 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.