Home > Mobile >  Is there a typedef for "unsigned long long int" in C?
Is there a typedef for "unsigned long long int" in C?

Time:12-06

I want to use the method strtoull(...) but do I really have to type out unsigned long long whenever I use it?

The largest integer typedef I could find was uint64_t / size_t but it is not the same as unsigned long long.

I believe unsigned long long takes up to much space. Is there some sort of official and recognized shortcut for it among the community? Do I have to make my own type? What would be a good name for it? u128int_t or uLLong?

CodePudding user response:

You say you "need a type for unsigned long long int", but unsigned long long int is a type.

Apparently your concern is that the name unsigned long long int is too long to type. You have a point, but defining an alias for it is likely to cause more confusion than it's worth. Every knowledgeable C programmer knows what unsigned long long int means. Nobody knows what your alias means without looking it up, and even then they can't be sure the meaning won't change as the software evolves. If you want to use unsigned long long int, it's best to use unsigned long long int (or unsigned long long).

You can define your own typedef. Remember that typedef doesn't define a new type. It only defines a new name for an existing type.

uint64_t, defined in <stdint.h>, may or may not be an alias for unsigned long long int, depending on the implementation. unsigned long long int is guaranteed to be at least 64 bits, but it could be wider (though I know of no implementations where it's not exactly 64 bits wide). Similarly, uintmax_t is likely to be unsigned long long int, but that's not guaranteed either.

You can define an alias if you like, but I wouldn't recommend it unless you need a name for some type that just happens to be defined as unsigned long long int. If you can give your typedef a name that has meaning within your application, that's probably a good idea. If its only meaning is "a shorter name for unsigned long long, I'd advise against it.

If you need a 64-bit unsigned integer type, uint64_t already exists; just use that. (And if the implementation doesn't provide a type with the required characteristics, then it won't define uint64_t and the error message when you try to use it will tell you that.)

CodePudding user response:

Do I have to make my own type?

You can typedefine whatever name you wish for whatever type (inside the defined naming rules of course), keep in mind that this is only an alias for the original type, hidding predefined types behind typedefs is not consensual, it is legal though.

u128int_t or uLLong?

Taking the above paragraph in consideration, uLLong is perfectly fine. As of today there is no primitive 128 bit wide type in C, u128int_t would be misleading, I would avoid it.

uint64_t is guaranteed to have 64 bits, unsigned long long int is not, it has to have at least 64 bits, but it is not guaranteed by any rule that it should have only that.

CodePudding user response:

Some compilers (for example gcc) support 128bit integers as an extension

example:

__int128_t mul(__int128_t x, __int128_t y)
{
    return x * y;
    
}

https://godbolt.org/z/c8bW1xYdh

  •  Tags:  
  • c
  • Related