Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux

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.)

like image 230
Harish Kayarohanam Avatar asked Apr 12 '15 18:04

Harish Kayarohanam


People also ask

Is long and long long the same?

long and long int are identical. So are long long and long long int . In both cases, the int is optional.

Is long long int 64-bit?

Windows: long and int remain 32-bit in length, and special new data types are defined for 64-bit integers.

Are long and long int the same?

But before starting the blog post, I want to make you clear that long and long int are identical and also long long and long long int. In both cases, the int is optional. There are several shorthands for built-in types. Let's see some examples of signed built-in types.

Does C support long long?

Yes GCC does support long long int , which is a part of C99 standard.


1 Answers

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).

like image 70
John Bollinger Avatar answered Oct 06 '22 00:10

John Bollinger