Click here to Skip to main content
15,919,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying in c
C++
main()
{
    int a=5;
    a=a++ + ++a + a++ + ++a + ++a;
    printf("%d",a);
}

I am trying more expression with it but not sufficient answer. so what is the method
for solve this type of expression. how gcc compiler work with this type of expression.
please
Posted
Updated 29-Aug-12 10:43am
v2

Left to right order, multiple passes...
Prefix operators first.
Postfix operators last.

And for extra credit...
Some compilers are buggy in this regard. It can be dangerous to write code in this form. In the real world, never do this. Always break such operations down into constituent parts, so you don't risk losing your job over a compiler bug.
 
Share this answer
 
v2
With most decent compilers your code is equivalent to this:
C++
main()
{
    int a=5;

    ++a; ++a; ++a;          // perform prefix increment/decrement instructions
    // a == 8
    a=a + a + a + a + a;    // execute the statement without increment/decrement operators
    // a == 40
    a++; a++;               // perform postfix increment/decrement instructions
    // a == 42

    printf("%d",a);
}

However I wouldn't be surprised if the C standard didn't define the value of a after running this terrible piece of code. Don't write such crap. I have never seen similar code in practice.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900