Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: long long always 64 bit?

Tags:

c

If i'm using long longs in my code, can i absolutely 100% guarantee that they will have 64 bits no matter what machine the code is run on?

like image 704
John Avatar asked Mar 07 '11 15:03

John


People also ask

Is Long Long always 64-bit?

They are guaranteed to be a minimnum of 64 bits. It's theoretically possible that they could be larger (e.g., 128 bits) though I'm reasonably they're only 64 bits on anything currently available. Save this answer.

How many bits is a long long?

The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits.

Is long long supported in C?

In C and C++, there are four different data type available for holding the integers i.e., short, int, long and long long.

Is unsigned long 32-bit or 64-bit?

LLP64 or 4/4/8 ( int and long are 32-bit, pointer is 64-bit)


2 Answers

No, C99 standard says that it will have at least 64 bits. So it could be more than that at some point I guess. You could use int64_t type if you need 64bits always assuming you have stdint.h available (standard in C99).

#include <stdint.h>
int64_t your_i64;
like image 72
Pablo Santa Cruz Avatar answered Sep 22 '22 22:09

Pablo Santa Cruz


You can test if your compiler is C99 complying with respect to numbers in the preprocessor with this

# if (~0U < 18446744073709551615U)
#  error "this should be a large positive value, at least ULLONG_MAX >= 2^{64} - 1"
# endif

This works since all unsigned values (in the preprocessor) are required to be the same type as uintmax_t and so 0U is of type uintmax_t and ~0U, 0U-1 and -1U all are the maximum representable number.

If this test works, chances are high that unsigned long long is in fact uintmax_t.

For a valid expression after the preprocessing phase to test this with the real types do

unsigned long long has_ullong_max[-1 + 2*((0ULL - 1) >= 18446744073709551615ULL)];

This does the same sort of trick but uses the postfix ULL to be sure to have constants of type unsigned long long.

like image 23
Jens Gustedt Avatar answered Sep 18 '22 22:09

Jens Gustedt