c# 如何在C中识别整数提升和降级?

gv8xihay  于 2022-12-09  发布在  C#
关注(0)|答案(1)|浏览(174)

我正在学习C语言,整数提升和术语“降级”对我来说是新的。我在C标准(C17)中读到了关于C类型转换和整数提升的内容,但我不知道如何识别整数提升(IP),我对降级一无所知。
例如,如果我有以下代码行:

...
unsigned char x;
int y;
long int z;
int aux; //Auxiliary variable

x='c';
y=1;
z=2;

aux=x;
aux=y;
aux=z;

aux=x+y; //ZX
aux=y+z;
...

我在哪里有整数提升呢?因为据我所知,在用“ZX”注解的代码行中,我有IP,但只有在那一行中,但我不确定;你能为我澄清一下吗?
你能给予我一些例子说明什么时候“降级”存在吗?因为C标准没有阐明。

wn9m85ua

wn9m85ua1#

肯定不是我的专长,但让我试试:

unsigned char x;
int y;
long int z;
int aux; //Auxiliar variable

x='c';
y=1;
z=2;    // Very subtle promotion here. 2 is an int, and is promoted to long int to fill z.

aux=x;  // Value of x, which is a char, is PROMOTED to int, to fill aux
aux=y;  // Both left and right sides are int, no promotion/demotion
aux=z;  // Z is a long int, is demoted to fill aux.

aux=x+y; // x(char) is PROMOTED for addition, to match other operand y(int)
aux=y+z; // y(int) is promoted to match longer type z(long int). The result(long int) is then demoted to match assignment aux.

我可能是错的。我在编程时尽量不过分依赖提升规则。

相关问题