Is there a shorthand for the unsigned long long type?

276 Views Asked by At

The type:

unsigned long long my_num;

seems cumbersome. I seem to recall seeing a shorthand for it, something like:

ull my_num;

Am I going mad, or is there a simpler way of writing unsigned long long?

4

There are 4 best solutions below

0
Damian Rhodes On BEST ANSWER

There is no type named ull. ull can be used as a suffix when specifying an integer constant such as:

unsigned long long my_num = 1ull;

You can read more about integer constants in the C standard section 6.4.4.1.

If you insist on using ull as a type name, although I would advise against this*, you can use typedef:

#include <stdio.h>
 
typedef unsigned long long ull;
int main() {
    ull a = 20ull;
    printf("%llu\n", a);
}

*As others have pointed out there is no clear advantage to using such a shorthand and at the very least it makes the code less readable.

0
Sparkling Marcel On

You can use ULL, but only on the right side of the assignement as long as I remember

Example: unsigned long long myValue = 123456789ULL;

but ULL myValue = 123456789ULL; shouldn't work

5
JD74 On

I think this is what your looking for.

stdint.h

int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t

0
chux - Reinstate Monica On

is there a simpler way of writing unsigned long long (?)

Yes, depending on goal.

First ask why is code using unsigned long long?

Use uintmax_t from <inttypes.h>
If the goal is to use the widest available integer type, then use uintmax_t. uintmax_t and unsigned long long types are at least 64-bit. uintmax_t is the widest available specified integer type and may be wider than unsigned long long.

Use uint64_t from <stdint.h>
If the goal is to use a 64-bit integer type, then use uint64_t. It is exactly 64-bits. unsigned long long may be 64-bits or wider.

Use unsigned long long
If the goal really is to unsigned long long, then use unsigned long long and not a shorthand or some typedef such as typedef unsigned long long ull;. In forming this answer, I did not type unsigned long long, but used copy & paste. unsigned long long is clear. ull is not a universal nor standard substitute for unsigned long long and such shorthands lead to less clear and non-portable code due to name collisions and the need to find the ull typedef to discern if it is as hoped.