75

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);
WilliamKF
  • 41,123
  • 68
  • 193
  • 295

9 Answers9

69

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
}
  • 10
    OP, make this the accepted answer already! Though I'd advise using just `_uc`, for conciseness as well as parity with the standard `ul` and so on. – underscore_d Nov 02 '17 at 20:15
32

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
26

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.

janm
  • 17,976
  • 1
  • 43
  • 61
  • I think you are the only one that decrypted his pretty secret question correctly. Firstly, i didn't figure out either what he really wanted to ask. – Johannes Schaub - litb Feb 21 '10 at 13:44
  • Right, that is my current work around, with this question I was hoping to avoid the cast, but it sounds like the answer is there is no literal that will allow me to avoid the cast. – WilliamKF Feb 21 '10 at 16:07
  • with the constructor syntax, does this cause an object allocation every time as opposed to the literal? – Ravi Nankani Nov 29 '16 at 03:19
  • @RaviNankani It's a constructor syntax for a primitive type, so there is should be no overhead. – janm Nov 29 '16 at 05:43
  • 2
    I'd write something like this (as the **uint8_t** typedef has been part of the standard since C++11): `min(uint8_t{9}, example2);` – dan_din_pantelimon Dec 07 '17 at 03:27
16

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
11

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);
E.M.
  • 4,498
  • 2
  • 23
  • 30
3

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

Kyle Lutz
  • 7,966
  • 2
  • 20
  • 23
1

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

Axel Gneiting
  • 5,293
  • 25
  • 30
  • 2
    This is not unsigned char, but signed char. – WilliamKF Feb 21 '10 at 05:10
  • 14
    @WilliamKF: the type of `'\0'` in C++ is neither `signed char` not `unsigned char`, it is `char` as Axel says. `char` is unlike other integral types: `signed int` and `int` are the same type, but `signed char` and `char` are not. – Steve Jessop Feb 21 '10 at 12:27
  • I thought `char` was implicitly the same as `signed char`, but I may be thinking of C... – Scott Smith Dec 18 '20 at 23:59
  • 2
    @ScottSmith, it's the same thing in C, also three distinct types. It's implementation defined if `char` is signed or unsigned. – Bo R Mar 05 '21 at 09:43
0

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.

user261840
  • 168
  • 2
-3

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.

Clive
  • 269
  • 1
  • 14
  • No it's not taken to be unsigned, it's still `char` — neither `unsigned char` nor `signed char`. – Ruslan Jan 13 '17 at 11:16