How this Actually Works

ankit1703's Avatar, Join Date: Jan 2013
Newbie Member
int a=10,b;
b=a++ + ++a;

specially with a++ ?
0
ankit1703's Avatar, Join Date: Jan 2013
Newbie Member
int a=10,b;
b=a++ + ++a;
how this actually works ?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Whatever result you see is undefined because you are using modifiers more than once on the same variable in the same statement.
jupiterjones like this
0
iranjit's Avatar, Join Date: Jan 2013
Newbie Member
For prefix increment:

Code:
int a=10;
printf( "%d" , ++a);
The output will be 11. I am sure you understand it why?

For postfix increment:

Code:
int a=10;
printf( "%d" , a++);
The output will be 10. It is because, the value of a is returned first then only it is increased by 1.

Here is another example:

Code:
int a=10, b;
printf( "%d" , a++);    // 10 is displayed
++a;                   // a becomes 12 after this statement
In you example, it doesn't matter if you use prefix or postfix increment but there are cases when, prefix and postfix increment gives different result.
shabbir like this