user807566 user807566 - 1 year ago 78
C Question

Two Equal Signs in One Line?

Could someone please explain what this does and how it is legal C code? I found this line in this code:, which is a C implementation of the Vitter algorithm for Adaptive Huffman Coding

ArcChar = ArcBit = 0;

From the function:

void arc_put1 (unsigned bit)
ArcChar <<= 1;

if( bit )
ArcChar |= 1;

if( ++ArcBit < 8 )

putc (ArcChar, Out);
ArcChar = ArcBit = 0;

ArcChar is an
and ArcBit is an
unsigned char

Answer Source

The value of the expression (a = b) is the value of b, so you can chain them this way. They are also right-associative, so it all works out.


ArcChar = ArcBit = 0;

is (approximately1) the same as

ArcBit = 0;
ArcChar = 0;

since the value of the first assigment is the assigned value, thus 0.

Regarding the types, even though ArcBit is an unsigned char the result of the assignment will get widened to int.

1   It's not exactly the same, though, as R.. points out in a comment below.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download