C++ int8_t 与 char ; 哪个是最好的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17744226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
int8_t vs char ; Which is the best one?
提问by sokid
My question may be confuse you, I know both are different types (signed char
and char
), however my company coding guidelines specifies to use int8_t
instead of char
.
我的问题可能会迷惑你,我知道,这两个是不同类型的(signed char
和char
),但我公司编码规范指定使用int8_t
代替char
。
So, I want to know, why I have to use int8_t
instead of char
type. Is there any best practices to use int8_t
?
所以,我想知道,为什么我必须使用int8_t
而不是char
类型。是否有任何最佳做法可供使用int8_t
?
回答by Mats Petersson
The use of int8_t
is perfectly good for some circumstances - specifically when the type is used for calculations where a signed 8-bit value is required. Calculations involving strictly sized data [e.g. defined by external requirements to be exactly 8 bit in the result] (I used pixel colour levels in a comment above, but that really would be uint8_t
, as negative pixel colours usually don't exist - except perhaps in YUV type colourspace).
在int8_t
某些情况下,使用是非常好的 - 特别是当该类型用于需要有符号 8 位值的计算时。涉及严格大小数据的计算[例如由外部要求定义为结果中的 8 位](我在上面的评论中使用了像素颜色级别,但确实如此uint8_t
,因为通常不存在负像素颜色 - 除了可能在YUV 类型色彩空间)。
The type int8_t
should NOT be used as a replacement of char
in for strings. This can lead to compiler errors (or warnings, but we don't really want to have to deal with warnings from the compiler either). For example:
该类型int8_t
不应用作char
字符串的in替换。这可能会导致编译器错误(或警告,但我们也不想处理来自编译器的警告)。例如:
int8_t *x = "Hello, World!\n";
printf(x);
may well compile fine on compiler A, but give errors or warnings for mixing signed and unsigned char values on compiler B. Or if int8_t
isn't even using a char
type. That's just like expecting
可能在编译器 A 上编译得很好,但在编译器 B 上混合有符号和无符号字符值时会给出错误或警告。或者int8_t
甚至不使用char
类型。这就像期待
int *ptr = "Foo";
to compile in a modern compiler...
在现代编译器中编译...
In other words, int8_t
SHOULD be used instead of char
if you are using 8-bit data for caclulation. It is incorrect to wholesale replace all char
with int8_t
, as they are far from guaranteed to be the same.
换句话说,如果您使用 8 位数据进行计算,int8_t
则应使用 SHOULD 而不是char
。这是不正确的批发更换所有char
有int8_t
,因为他们远离保证是相同的。
If there is a need to use char
for string/text/etc, and for some reason char
is too vague (it can be signed or unsigned, etc), then usign typedef char mychar;
or something like that should be used. (It's probably possible to find a better name than mychar
!)
如果需要使用char
字符串/文本/等,并且由于某种原因char
太模糊(它可以是有符号或无符号等),那么typedef char mychar;
应该使用usign或类似的东西。(可能找到比 更好的名字mychar
!)
Edit: I should point out that whether you agree with this or not, I think it would be rather foolish to simply walk up to whoever is in charge of this "principle" at the company, point at a post on SO and say "I think you're wrong". Try to understand what the motivation is. There may be more to it than meets the eye.
编辑:我应该指出,无论您是否同意这一点,我认为简单地走到公司中负责这项“原则”的人面前,指着 SO 上的帖子说“我认为你错了”。尝试了解动机是什么。它可能比我们看到的更多。
回答by Stephen Canon
They simply make different guarantees:
他们只是做出不同的保证:
char
is guaranteed to exist, to be at least 8 bits wide, and to be able to represent either all integers between -127 and 127 inclusive (if signed) or between 0 and 255 (if unsigned).
char
保证存在,至少为 8 位宽,并且能够表示 -127 到 127(如果有符号)或 0 到 255(如果无符号)之间的所有整数。
int8_t
is not guaranteed to exist (and yes, there are platforms on which it doesn't), but if it exists it is guaranteed to an 8-bit twos-complement signed integer type with no padding bits; thus it is capable of representing all integers between -128 and 127, and nothing else.
int8_t
不保证存在(是的,有些平台不存在),但如果存在,则保证为 8 位二进制补码有符号整数类型,没有填充位;因此它能够表示 -128 到 127 之间的所有整数,仅此而已。
When should you use which? When the guarantees made by the type line up with your requirements. It is worth noting, however, that large portions of the standard library require char *
arguments, so avoiding char
entirely seems short-sighted unless there's a deliberate decision being made to avoid usage of those library functions.
什么时候应该使用哪个?当类型所做的保证符合您的要求时。然而,值得注意的是,标准库的大部分内容都需要char *
参数,因此char
完全避免似乎是短视的,除非有意做出避免使用这些库函数的决定。
回答by Pete Becker
int8_t
is onlyappropriate for code that requires a signed integer type that is exactly8 bits wide and should not compile if there is no such type. Such requirements are far more rare than the number of questions about int8_t
and it's brethren indicates. Most requirements for sizes are that the type have at leasta particular number of bits. signed char
works just fine if you need at least 8 bits; int_least8_t
also works.
int8_t
是仅适合于该要求有符号整数类型,它是代码恰好宽8位,并且如果不存在这样的类型不应编译。这样的要求远比关于int8_t
它的兄弟们所指出的问题数量少得多。对大小的大多数要求是该类型至少具有特定数量的位。signed char
如果您需要至少 8 位,则工作正常;int_least8_t
也有效。
回答by Sneftel
int8_t
is specified by the C99 standard to be exactly eight bits wide, and fits in with the other C99 guaranteed-width types. You should use it in new code where you want an exactly 8-bit signed integer. (Take a look at int_least8_t
and int_fast8_t
too, though.)
int8_t
由 C99 标准指定为正好 8 位宽,并且适合其他 C99 保证宽度类型。您应该在需要精确 8 位有符号整数的新代码中使用它。(不过int_least8_t
,int_fast8_t
也看看and 。)
char
is still preferred as the element type for single-byte character strings, just as wchar_t
should be preferred as the element type for wide character strings.
char
仍然首选作为单字节字符串的元素类型,就像wchar_t
应该首选作为宽字符串的元素类型一样。