C语言uint8

编程入门 行业动态 更新时间:2024-10-27 09:41:34

C<a href=https://www.elefans.com/category/jswz/34/1770116.html style=语言uint8"/>

C语言uint8

鉴于这个C 11计划,我应该期待看到一个数字还是一个字母?还是没有期望?

#include

#include

int main()

{

int8_t i = 65;

std::cout << i;

}

标准是否指定此类型是否可以是字符类型?

解决方法:

根据C 0x FDIS(N3290)的§18.4.1[cstdint.syn],int8_t是一个可选的typedef,其指定如下:

namespace std {

typedef signed integer type int8_t; // optional

//...

} // namespace std

§3.9.1[basic.fundamental]陈述:

There are five standard signed integer types: “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types.

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type.

§3.9.1还规定:

In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.

很有可能得出结论,int8_t可能是char的typedef,前提是char对象采用有符号值;但是,情况并非如此,因为char不在有符号整数类型列表中(标准和可能扩展的有符号整数类型).另请参见std :: make_unsigned和std :: make_signed上的Stephan T. Lavavej’s comments.

因此,int8_t是signed char的typedef,或者是扩展的有符号整数类型,其对象恰好占用8位存储.

但是,要回答你的问题,你不应该做出假设.因为已经定义了x.operator<

>模板< class traits> basic_ostream&LT炭,性状&GT&安培;如果int8_t是签名字符的完全匹配(即signed char的typedef),则将调用operator&,signed char)模板.

>否则,int8_t将被提升为int和basic_ostream< charT,traits>&将调用operator

在std :: cout<

>模板< class traits> basic_ostream&LT炭,性状&GT&安培;如果uint8_t是unsigned char的完全匹配,则将调用operator&,unsigned char)模板.

>否则,因为int可以表示所有uint8_t值,uint8_t将被提升为int和basic_ostream< charT,traits>&将调用operator

如果您总是想要打印一个角色,最安全,最明确的选择是:

std::cout << static_cast(i);

如果你总想打印一个数字:

std::cout << static_cast(i);

标签:c,c11,language-lawyer,iostream,standard-library

来源: .html

更多推荐

C语言uint8

本文发布于:2023-07-03 09:23:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1004391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!