如何在C ++中指定unsigned char类型的整数文字? [英] How do I specify an integer literal of type unsigned char in C++?

查看:102
本文介绍了如何在C ++中指定unsigned char类型的整数文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以指定unsigned long类型的整数文字,如下所示:

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);

我希望避免我目前拥有的冗长解决方法,并且没有出现'unsigned char'调用min的行而不在另一行的变量中声明9:

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没有提供指定整数常量的标准方法宽度小于 int 类型。

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

然而, stdint.h 确实提供了 UINT8_C()宏来做一些与你正在寻找的内容非常接近的内容。

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.

但大多数人只使用无后缀(获得 int 常量)或 U 后缀(获取 unsigned int 常量)。它们可以很好地处理字符大小的值,而且无论如何,这几乎都可以从 stdint.h 宏中获得。

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.

这篇关于如何在C ++中指定unsigned char类型的整数文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆