Harish Kayarohanam
Harish Kayarohanam

Reputation: 4024

Do long long and long have same range in C in 64-bit machine?

I saw in /usr/include/limits.h as

/* Minimum and maximum values a `signed long int' can hold.  */
if __WORDSIZE == 64
 define LONG_MAX     9223372036854775807L
else
 define LONG_MAX     2147483647L
endif
define LONG_MIN      (-LONG_MAX - 1L)
/* Maximum value an `unsigned long int' can hold.  (Minimum is 0.)  */
if __WORDSIZE == 64
 define ULONG_MAX    18446744073709551615UL
else
 define ULONG_MAX    4294967295UL
endif
ifdef __USE_ISOC99

/* Minimum and maximum values a `signed long long int' can hold.  */
define LLONG_MAX    9223372036854775807LL
define LLONG_MIN    (-LLONG_MAX - 1LL)

/* Maximum value an `unsigned long long int' can hold.  (Minimum is 0.)    */
 define ULLONG_MAX   18446744073709551615ULL

So unsigned long long int and unsigned long int seem to have same max value of 18446744073709551615 ...

Does that mean long long means same as long in this machine? If so, why then do we have a separate long long specifier?

From reading C books, I expected long long to be double the size of long. Am I going wrong somewhere? (But yes, I agree with standards that int >= short int, long >= int, but it is hard to digest long long >= long.)

Upvotes: 5

Views: 2825

Answers (3)

Serge Ballesta
Serge Ballesta

Reputation: 149145

C has two kinds of integral types :

  • traditionals : short, int, long, long long. It is up to each implementation to define what size those types are. C just specifies that sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long) (ref 6.3.1.1 in final draft of C1X). With those types you can never be sure which one is 32 or 64 bits long because it is explicitly implementation dependent
  • explicits : int8_t, int16_t, int32_t, int64_t - when they exist you can be sure of their size. Standard say they should be declared in stdint.h, but unfortunately, they are all optional - that means you cannot be sure they exist even if all major implementation support int16_t, int32_t and int64_t (ref : 7.20 and 7.20.1.1 of above draft)

And ... there is no recommended relation between traditionals and explicits types (note the appellations traditionals and explicits are mine).

7.20.1 of same draft defines other interesting types :

  • intptr_t and uintptr_t : resp. signed and unsigned integral types guaranteed to be large enough to hold a pointer (unluckily, those types are optionals ...)
  • int_leastX_t : a type of at least X bits
  • int_fastX_t : the fastest type of at least X bits

The following types are required (meaning they shall exist in any conformant implementation) :

int_least8_t int_least16_t int_least32_t int_least64_t uint_least8_t uint_least16_t uint_least32_t uint_least64_t

int_fast8_t int_fast16_t int_fast32_t int_fast64_t uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t

  • intmax_t and uintmax_t : signed (resp unsigned) integer type capable of representing any value of any signed (resp unsigned) integer type - required

Upvotes: 4

R Sahu
R Sahu

Reputation: 206717

You asked:

so does that mean long long means same as long in this machine

The type names are different even when they are of the same size.

so why then do we have a separate long long specifier ? `

The language specifies them as different types. It's up to a platform what size of integral number to use to support them. In the compiler you are using long long and long have the same size. It's possible for a different compiler on the same platform to use 64 bits for long long and 32 bits for long and int.

Upvotes: 3

John Bollinger
John Bollinger

Reputation: 181244

The C language specification specifies types that conforming implementations must provide. Type long [int] has been among them since the first version of the standard, and type long long [int] has been among them since C99.

The standard specifies relationships among the sizes of the various integer types, and in particular that type long long must be able to represent all the values that type long can represent, but it leaves many aspects of type characteristics "implementation defined", meaning implementations choose (and are obligated to document their choice). Among those characteristics are whether there are any long long values that are outside the range of long.

At the level of the standard, none of that is directly related to the native word size of the implementation's target machine, but implementations are free to use that as a criterion when they choose type representations.

If you care about the range of values that various types you use can represent, then use the fixed-width types from stdint.h (int64_t, uint32_t, etc.), or use the standard macros giving the integer type limits, (INT_MAX and friends).

Upvotes: 6

Related Questions