How do I specify an integer literal of type unsigned char in C++?

C++IntegerConstantsLanguage Lawyer

C++ Problem Overview


I can specify an integer literal of type unsigned long as follows:

const unsigned long example = 9UL;

How do I do likewise for an unsigned char?

const unsigned char example = 9U?;

This is needed to avoid compiler warning:

unsigned char example2 = 0;
...
min(9U?, example2);

I'm hoping to avoid the verbose workaround I currently have and not have 'unsigned char' appear in the line calling min without declaring 9 in a variable on a separate line:

min(static_cast<unsigned char>(9), example2);

C++ Solutions


Solution 1 - C++

C++11 introduced user defined literals. It can be used like this:

inline constexpr unsigned char operator "" _uchar( unsigned long long arg ) noexcept
{
    return static_cast< unsigned char >( arg );
}

unsigned char answer()
{
    return 42;
}

int main()
{
    std::cout << std::min( 42, answer() );        // Compile time error!
    std::cout << std::min( 42_uchar, answer() );  // OK
}

Solution 2 - C++

C provides no standard way to designate an integer constant with width less that of type int.

However, stdint.h does provide the UINT8_C() macro to do something that's pretty much as close to what you're looking for as you'll get in C.

But most people just use either no suffix (to get an int constant) or a U suffix (to get an unsigned int constant). They work fine for char-sized values, and that's pretty much all you'll get from the stdint.h macro anyway.

Solution 3 - C++

You can cast the constant. For example:

min(static_cast<unsigned char>(9), example2);

You can also use the constructor syntax:

typedef unsigned char uchar;
min(uchar(9), example2);

The typedef isn't required on all compilers.

Solution 4 - C++

If you are using Visual C++ and have no need for interoperability between compilers, you can use the ui8 suffix on a number to make it into an unsigned 8-bit constant.

min(9ui8, example2);

You can't do this with actual char constants like '9' though.

Solution 5 - C++

Assuming that you are using std::min what you actually should do is explicitly specify what type min should be using as such

unsigned char example2 = 0;
min<unsigned char>(9, example2);

Solution 6 - C++

Simply const unsigned char example = 0; will do fine.

Solution 7 - C++

I suppose '\0' would be a char literal with the value 0, but I don't see the point either.

Solution 8 - C++

There is no suffix for unsigned char types. Integer constants are either int or long (signed or unsigned) and in C99 long long. You can use the plain 'U' suffix without worry as long as the value is within the valid range of unsigned chars.

Solution 9 - C++

The question was how to "specify an integer 'literal' of type unsigned char in C++?". Not how to declare an identifier.

You use the escape backslash and octal digits in apostrophes. (eg. '\177')

The octal value is always taken to be unsigned.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilliamKFView Question on Stackoverflow
Solution 1 - C++user4832129View Answer on Stackoverflow
Solution 2 - C++Michael BurrView Answer on Stackoverflow
Solution 3 - C++janmView Answer on Stackoverflow
Solution 4 - C++Mr ListerView Answer on Stackoverflow
Solution 5 - C++E.M.View Answer on Stackoverflow
Solution 6 - C++Kyle LutzView Answer on Stackoverflow
Solution 7 - C++Axel GneitingView Answer on Stackoverflow
Solution 8 - C++user261840View Answer on Stackoverflow
Solution 9 - C++CliveView Answer on Stackoverflow