加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C .为什么std :: cout << char int打印int值?

发布时间:2020-12-16 07:17:59 所属栏目:百科 来源:网络整理
导读:比方说,我们有: char x = 'a';int y = 1; 所以,如果你运行: std::cout x + y; 它打印98而不是’b’.正如我从here看到的那样 operator只有int参数实现. 从现在开始,我有两个问题: char int操作后返回什么类型? 为什么没有char参数实现,但是std :: cout x
比方说,我们有:

char x = 'a';
int y = 1;

所以,如果你运行:

std::cout << x + y;

它打印98而不是’b’.正如我从here看到的那样
<< operator只有int参数实现. 从现在开始,我有两个问题:
> char int操作后返回什么类型?
>为什么没有char参数实现,但是std :: cout<< x仍然按预期工作并打印char值?

解决方法

感谢 Fefux,Bo Persson和 Matti Virkkunen的答案是:

>从CPP Reference: Implicit conversions开始:

arithmetic operators do not accept types smaller than int as
arguments,and integral promotions are automatically applied after
lvalue-to-rvalue conversion,if applicable.

所以x y的返回类型是int.
> std :: cout有一个运算符<<(char)作为non-member.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读