C11隐式转换字符,x%y

在C11中,

char foo(char x, char y) { return x % y; } 

是否在任何地方进行了隐式转换(例如升级到int)?

通常的算术转换适用于%的操作数,与它们应用于其他乘法和加法运算符的操作数的方式相同(参见6.5.5乘法运算符 )。

操作数被提升为intunsigned int ,具体取决于平台上char类型的范围(参见6.3.1.8常用算术转换6.3.1.1布尔,字符和整数

在大多数现实生活平台上,操作数将被提升为int ,因为int范围通常涵盖整个char范围。 在一个或多或少的异国情调的平台上, sizeof(char) == sizeof(int)char是无符号类型(暗示char范围不适合int范围),它们将被提升为unsigned int

%运算符的操作数的要求是
n1570-§6.5.5(P2):

%运算符的操作数应具有整数类型

在这种情况下,当char被提升为unsigned int通常会在两个操作数上进行算术转换。

是的,char通常被提升为int或unsigned int,但如果解释器可以certificate结果是相同的,则可以直接使用chars进行计算:

引用标准, 5.1.2.3 Program execution

11示例2执行片段

 char c1, c2; /* ... */ c1 = c1 + c2; 

”整数提升”要求抽象机将每个变量的值提升为int大小,然后添加两个整数并截断总和。

Provided the addition of two chars can be done without §5.1.2.3 Environment 15ISO/IEC 9899:201x Committee Draft — April 12, 2011 N1570 overflow, or with overflow wrapping silently to produce the correct result, the actual execution need only produce the same result, possibly omitting the promotions