简体   繁体   English

*= 有什么作用?

[英]What does *= do?

Hey I am kinda new to C and I wanted to ask why this prints out 4 instead of 260 ?嘿,我对 C 有点陌生,我想问一下为什么打印出来的是4而不是260

#include <stdio.h>

int main()
{
    unsigned char x = 130;
    x *= 2;
    printf("%d\n", x);
}

The *= operator is called multiplication assignment operator and is shorthand for multiplying the operand to the left with the operand to the right and assigning the result to the operand to the left. *=运算符称为乘法赋值运算符,它是将左侧的操作数与右侧的操作数相乘并将结果分配给左侧的操作数的简写。 In this case, it's the same as:在这种情况下,它与以下内容相同:

x = x * 2;

An unsigned char can usually only carry values between 0 and 255 (inclusive) so the result overflows (wraps around) when you try assigning values above 255 and 260 % 256 == 4. unsigned char通常只能携带 0 到 255(含)之间的值,因此当您尝试分配高于 255 和 260 % 256 == 4 的值时,结果会溢出(回绕)。

According to language specification unsigned char data type takes 1 byte of memory so it can store values 0-255 and if you a value greater than 255 it will overflow beginning from zero.根据语言规范,无符号字符数据类型占用 memory 的 1 个字节,因此它可以存储值 0-255,如果值大于 255,它将从零开始溢出。 So 260(130 * 2) - 256 = 4 is assigned to your variable.所以 260(130 * 2) - 256 = 4 被分配给你的变量。 https://docs.microsoft.com/en-us/cpp/cpp/cpp-type-system-modern-cpp?view=msvc-170#fundamental-built-in-types https://docs.microsoft.com/en-us/cpp/cpp/cpp-type-system-modern-cpp?view=msvc-170#fundamental-built-in-types

In the statement with the compound assignment operator在带有复合赋值运算符的语句中

x*=2;

that is equivalent to相当于

x = x * 2;

the operand x of the expression is converted to the type int due to the integer promotions and the result pf the expression is indeed equal to 260. But the result is assigned to the variable x that has the type unsigned char.由于 integer 促销活动,表达式的操作数x被转换为int类型,并且表达式的结果确实等于 260。但结果被分配给类型为 unsigned char 的变量 x。

unsigned char x=130;

The value 260 can not be stored in such an object.值 260 不能存储在这样的 object 中。 As 260 is internally as an integer is represented like由于 260 在内部是 integer 表示为

0x00000104

then only the last byte having the value 0x4 is stored in the object and this value is outputted.然后只有最后一个值为 0x4 的字节存储在 object 中,并输出该值。

You could get the expected result if for example the type of the variable x would be changed at least like例如,如果变量 x 的类型至少会改变,您可以获得预期的结果

unsigned short x=130;

x *= 2 uses the multiplication assignment operator , a short hand notation for x = x * 2 where x is only evaluated once. x *= 2使用乘法赋值运算符,这是x = x * 2的简写符号,其中x只计算一次。

The behavior of this small program is non trivial:这个小程序的行为很重要:

  • The multiplication is performed after integer promotion of the arguments, which means the value of x , 130 is promoted to type int and the multiplication gives 260 as an int .乘法是在 arguments 的 integer 提升之后执行的,这意味着x的值, 130提升为int类型,并且乘法得到260作为int
  • This value is converted to the destination type, unsigned char by repeatedly subtracting UCHAR_MAX , which is probably 256 on your system, until reaching a value in the range of the destination type.该值通过反复减去UCHAR_MAX (在您的系统上可能为 256)转换为目标类型unsigned char ,直到达到目标类型范围内的值。 Hence x becomes 260 % 256 = 4.因此x变为 260 % 256 = 4。
  • Because it has an integer type smaller than int , x is promoted to int when passed to printf , so the format %d , which expects an int value, has defined behavior and produces 4 .因为它有一个小于int的 integer 类型,所以x在传递给printf时被提升为int ,所以格式%d ,它需要一个int值,已经定义了行为并产生4

Note that for exotic architectures (eg: digital signal processing chips) where unsigned char has more than 8 bits, some of the above discussion is irrelevant and the printf may print 260 or even have undefined behavior (if sizeof(int) == 1 ).请注意,对于unsigned char超过 8 位的奇异架构(例如:数字信号处理芯片),上面的一些讨论是不相关的,并且printf可能会打印260甚至具有未定义的行为(如果sizeof(int) == 1 ) .

The unsigned char can contain only from 0 to 256. When the value is over 256, the value is forced from 0 to 256. unsigned char只能包含 0 到 256。当值超过 256 时,该值强制从 0 到 256。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM