为什么编译器在printf中将char转换为int?

我正在使用https://cppinsights.io/。以下是代码(默认示例)

#include <cstdio>

int main()
{
    const char arr[10]{2,4,6,8};
    for(const char& c : arr)
    {
      printf("c=%cn", c);
    }
}

关于cppinsights

C++ Insights 是一个基于 clang 的工具,可以进行源到源的转换。它的目标是让事情可见,这通常是有意地在幕后发生。这是关于编译器为我们做的事情的魔法。或者查看编译器的类。

这是生成的代码,这是编译器如何看待代码

#include <cstdio>

int main()
{
  const char arr[10] = {2, 4, 6, 8, '', '', '', '', '', ''};
  {
    char const (&__range1)[10] = arr;
    const char * __begin1 = __range1;
    const char * __end1 = __range1 + 10L;
    for(; __begin1 != __end1; ++__begin1) {
      const char & c = *__begin1;
      printf("c=%cn", static_cast<int>(c));
    }
  }
}

我的问题是,为什么要使用static_cast<int>即使%c

回答

在 C 中,任何比 an 更窄的参数在传递给 时int自动提升为,或者通常在传递给任何函数以用于声明中对应的参数或任何没有参数原型的函数(用 声明)。C++ Insights 明确地向您展示了此促销活动。intprintf...()

尽管%c转换说明符打印一个字符,但它希望在int参数中接收该字符。

晋升的原因主要是历史性的;C 是在本地寄存器大小的计算很容易并且为其他类型实现特定语义需要更多工作的环境中开发的。因此,在大多数表达式中,比int提升为更窄的整数int

  • Note that C++ also has template [parameter packs](https://en.cppreference.com/w/cpp/language/parameter_pack). They also use the `...` syntax, but there the promotion does not happen. Instead, the compiler instantiates the template with the correct types, which can be `char`. But that only works for native C++, not for functions inherited from C.

以上是为什么编译器在printf中将char转换为int?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>