Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Equal Signs in One Line?

Tags:

c

Could someone please explain what this does and how it is legal C code? I found this line in this code: http://code.google.com/p/compression-code/downloads/list, 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 )
        return;

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

ArcChar is an int and ArcBit is an unsigned char

like image 735
user807566 Avatar asked Aug 30 '11 13:08

user807566


People also ask

Can you have 2 equal signs in an equation?

Two equals signs means that the first quantity is equal to the second quantity, and the second quantity is equal to the third quantity, or that all three quantities are equal. No surprises. x+2=6, and 6=y+3, so x=4,y=2.

What does double == mean?

A double equal sign means “is equal to.” Notice the line above involving the double equal sign?

What does 2 equal signs mean?

0. This two equal signs, as you call them, compare what is on the left side to what is on the right side of them. If variable x "remembers" value 10, then the left side is equal to the right side and the expression (equation) is true.

What does === mean in code?

What does === triple equal means. Just like double equal operator === also used to compare two values on left and right. This will also return true or false based on comparison. Triple equal operator is also common used in if else conditions, while loops and some other places in code.


1 Answers

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.

Essentially

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.

like image 157
Joey Avatar answered Sep 22 '22 17:09

Joey