C语言uint8_t和char的区别,c – int8_t和uint8_t是char类型吗?
鑒于這個(gè)C 11計(jì)劃,我應(yīng)該期待看到一個(gè)數(shù)字還是一個(gè)字母?還是沒有期望?
#include
#include
int main()
{
int8_t i = 65;
std::cout << i;
}
標(biāo)準(zhǔn)是否指定此類型是否可以是字符類型?
解決方法:
根據(jù)C 0x FDIS(N3290)的§18.4.1[cstdint.syn],int8_t是一個(gè)可選的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”, “l(fā)ong int”, and “l(fā)ong 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還規(guī)定:
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.
很有可能得出結(jié)論,int8_t可能是char的typedef,前提是char對象采用有符號(hào)值;但是,情況并非如此,因?yàn)閏har不在有符號(hào)整數(shù)類型列表中(標(biāo)準(zhǔn)和可能擴(kuò)展的有符號(hào)整數(shù)類型).另請參見std :: make_unsigned和std :: make_signed上的Stephan T. Lavavej’s comments.
因此,int8_t是signed char的typedef,或者是擴(kuò)展的有符號(hào)整數(shù)類型,其對象恰好占用8位存儲(chǔ).
但是,要回答你的問題,你不應(yīng)該做出假設(shè).因?yàn)橐呀?jīng)定義了x.operator<
>模板< class traits> basic_ostream<炭,性狀>&安培;如果int8_t是簽名字符的完全匹配(即signed char的typedef),則將調(diào)用operator&,signed char)模板.
>否則,int8_t將被提升為int和basic_ostream< charT,traits>&將調(diào)用operator
在std :: cout<
>模板< class traits> basic_ostream<炭,性狀>&安培;如果uint8_t是unsigned char的完全匹配,則將調(diào)用operator&,unsigned char)模板.
>否則,因?yàn)閕nt可以表示所有uint8_t值,uint8_t將被提升為int和basic_ostream< charT,traits>&將調(diào)用operator
如果您總是想要打印一個(gè)角色,最安全,最明確的選擇是:
std::cout << static_cast(i);
如果你總想打印一個(gè)數(shù)字:
std::cout << static_cast(i);
標(biāo)簽:c,c11,language-lawyer,iostream,standard-library
來源: https://codeday.me/bug/20190923/1813451.html
總結(jié)
以上是生活随笔為你收集整理的C语言uint8_t和char的区别,c – int8_t和uint8_t是char类型吗?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [[机缘参悟-87]:每个人需要了解自己
- 下一篇: 文档管理利器--云脉文档自动分类快速检索