Course contentsShow
← C
Lesson 214 of 1,2193. Operators and ExpressionsFree lesson

Right-to-left associativity

Right-to-left associativity

What you'll learn: How to predict the order of operations when multiple operators of the same rank, like assignments, appear in a single line of code.

The "Direction of Traffic"

Most operators in C, like addition or subtraction, work from left to right. If you see 10 - 5 - 2, your brain naturally does the math from left to right (5−2=35 - 2 = 3). However, a specific group of operators—most notably the assignment operator (=)—prefers to drive on the other side of the road. They work from right to left.

Think of a relay race. In a left-to-right race, the baton starts at the beginning of the line and moves to the end. In right-to-left associativity, the "value" (the baton) starts at the very end of the expression and is passed backward toward the start of the line.

Chain Assignments

The most common place you will see this is when you want to set multiple variables to the same value at once. Instead of writing three separate lines, C allows you to chain them together. Because the = operator is right-associative, the computer starts at the far right and works its way back to the left.

#include <stdio.h>

int main() {
    int a, b, c;

    // Right-to-left in action:
    // 1. The value 50 is assigned to 'c'
    // 2. The result of (c = 50) is passed to 'b'
    // 3. The result of (b = 50) is passed to 'a'
    a = b = c = 50;

    printf("a: %d, b: %d, c: %d", a, b, c);
    
    return 0;
}

In the code above, the expression c = 50 happens first. That entire expression actually "results" in the value 50, which is then handed over to b, and finally to a. If C worked from left to right here, the code would break because a would try to grab a value from b before b had anything inside it!

Why it matters

Right-to-left associativity is also found in unary operators (operators that act on a single thing, like ++ or - for negative numbers). When you see a complex line like !!variable, C evaluates the right-most ! first, then the next one to its left. By understanding this "backward" flow, you can read complex assignment chains and nested logic without getting tangled up.

Key takeaway

Right-to-left associativity means that when operators have the same priority, C evaluates the expression starting from the right side and moves toward the left.