Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How this Actually Works (http://www.go4expert.com/forums/actually-t29393/)

ankit1703 1Jan2013 14:01

How this Actually Works
 
int a=10,b;
b=a++ + ++a;

specially with a++ ?

ankit1703 1Jan2013 14:02

Re: 100 Multiple choice questions in C
 
int a=10,b;
b=a++ + ++a;
how this actually works ?

xpi0t0s 2Jan2013 00:14

Re: How this Actually Works
 
Whatever result you see is undefined because you are using modifiers more than once on the same variable in the same statement.

iranjit 5Jan2013 18:22

Re: How this Actually Works
 
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.


All times are GMT +5.5. The time now is 12:06.