gpt4 book ai didi

c++ - char 是否默认提升?

转载 作者:行者123 更新时间:2023-12-02 10:50:39 24 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,但有人可以提供 C++11 和 C11 的标准引用吗:

char 是否默认提升为 int

这里有一些背景知识:C 和 C++ 都有默认参数提升的概念(C++11:5.2.2/7;C11:6.5.2.2/6)。这意味着在以下调用中,参数将被提升:

void f(int, ...);

float a = 1; short int b = 2; char c = 'x';

f(0, a, b, c);

对于函数调用,a 转换为 doubleb 转换为 int。但是c 会发生什么呢?我一直觉得char也升级为int,但在标准中找不到相关的说法。

最佳答案

首先,默认参数提升

6.5.2.2

If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions.

现在进行整数促销:

6.3.1.1

The following may be used in an expression wherever an int or unsigned int may be used:

  • An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

因此,对于 C 来说,至少有一个 char 默认提升为 intunsigned int

关于c++ - char 是否默认提升?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11985774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com