For this calculation, most programming languages yield a result of 5, but in C language, the result is 6. This article explains why.
One explanation comes from the perspective of assembly language, which shows the corresponding assembly code for the relevant C statements, illustrating how the assembly code computes the result of 6 by performing operations in a specific order. However, I feel that this explanation only describes how the result of 6 is obtained, without clarifying why C language computes it this way. Many might dismiss it with “it’s defined this way,” but why is it defined like this? Here, I share my thoughts.
First, let’s discuss the similarities between C language and other languages. They all follow the principle of operator precedence, calculating the values within the two parentheses first. Since the parentheses are evaluated from left to right, the value of i in the left parentheses is computed first, changing from its initial value of 1 to 2 after the increment. Next, the right parentheses are evaluated, where i has already become 2 from the previous increment, so it increments to 3. The issue arises in how the addition outside the last two parentheses is computed.

Most languages interpret the logic similarly to common reasoning, where the final addition is computed as the result of the first parentheses (i incremented to 2) plus the result of the second parentheses (i incremented to 3), yielding a final result of 5. However, in C language, the variable’s perspective is that “the same variable cannot meet at the same moment” and here, the “moment” is a clear, indivisible unit of time. Within this “moment,” a variable can only have one state, meaning it can only hold one value.
This “moment” refers to the duration of a “basic mathematical operation”. For example, the calculation of “a+b” occurs within an indivisible “moment”, while “a+b+c” consists of two moments: the first for calculating a+b, and the second for adding c.
In analyzing the expression (++i) + (++i), C language considers the results of both parentheses to be i, and simultaneously, they perform the third addition calculation within the smallest “atomic moment”. Thus, they must be the same because “the same variable cannot meet at the same moment“; at the same moment, there can only be one instance of the variable. If there are two, they must be the same value, like looking in a mirror. As for what this same value is? It is, of course, the latest computed result of i, which is 3. Therefore, the final result of the calculation is 6.
Most programming languages do not have C language’s strict definition of “indivisible atomic moments”. In their view, the i in the parentheses on either side of the addition is the same object but belongs to different periods, so having two values for one variable is not contradictory. A basic mathematical operation can connect two time states, allowing for the situation where both the addend and the augend are the same variable but different values.
Finally, here’s a thought experiment: still in C language, what is the result of (++i) + (++i) + (++i) when i=1?