gpt4 book ai didi

c - 什么是完全提升类型?

转载 作者:太空狗 更新时间:2023-10-29 17:12:10 24 4
gpt4 key购买 nike

我在 va_copy(3) 中遇到了这个:

/* need a cast here since va_arg only
* takes fully promoted types */
c = (char) va_arg(ap, int);

什么是完全提升的类型?

最佳答案

这是指整数提升的规则。任何时候在 int 的上下文中使用类型小于 int(即 charshort)的整数值code> 可以使用,该值被提升为 int

在可变参数函数的情况下,函数参数的类型在编译时是未知的,因此适用此提升。

例如,假设您有以下功能:

void f1(char c);
void f2(int count, ...);

他们的名字是这样的:

char x = 1;
f1(x); // x is passed as char
f2(1, x); // x is passed as int

此行为记录在 C standard 的第 6.3.1.1p2 节中:

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 .
  • A bit-field of type _Bool , int , signed int ,or 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 . All other types are unchanged by the integer promotions.

关于c - 什么是完全提升类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56586904/

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