What is the output for the code -
void main()
int i=4,x;

What's with the underscores? Is that a C11 thing? Ignoring the sytactical errors, the order of evaluation is compiler-specific. Until I see some valid (in my eye) code, I cannot answer your question. If what you meant was x = ++i + ++i + ++i; then anwser should be either 18 or 15. What does your system show? Mine returns 19 for x. If I change the code to this:

#include <stdio.h>

int main(void)
    int i=4,x;
    int i1 = ++i;
    int i2 = ++i;
    int i3 = ++i;
    x = i1 + i2 + i3;
//    x=++i + ++i + ++i; // Returns 19 as x.
    printf("i1 == %d, i2 == %d, i3 == %d, x == %d\n",i1, i2, i3, x); // Returns 18 as x, 5 as i1, 6 as i2, and 7 as i3.
    return 0;

The result of x is 18, as expected.

The lesson here is to NOT have functions (including increment operators) that operate on the same variable within a single expression. This will cause you more problems than you can imagine. I have spent days debugging these problems for colleagues in the past when their code was seriously broken.

If I just set x to ++i, the expected result of i == 5 and x == 5 is output. If I set x to ++i + ++i, the result is i == 6 (expected) but x is 12. This is telling me that the original value of I is used for each increment, not the incremented value of the other operand of the expression, resulting in a value of 12 instead of the expected 11 for x. This is not wrong since these decisions are compiler-dependent. Caveat programmer!

Note that I am using GCC 4.4.7.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.