# Thread: How Does Increment Operators Work...??

1. ## How Does Increment Operators Work...??

CAN ANY1 PLZ TELL ME HOW INCREMENT & DECREMENT OPERATORS WORK IN C AS WELL AS IN LINUX IF KNOWN....!!
AS FAR AS I KNWO THERE ARE POST & PRE INCREMENT/DECREMENT OPERATORS :
I M GETTING DIFF RESULTS WHEN CALCULATED MANUALLY & DIFFERENT WHEN PROGRAMMED IN C ....!!
SUPPOSE FOR EX :

a = 2b + (- - b) + 3(b++) +b

if b = 3
what is ans of b
manually ans is = 6 + 2 + (3*2) + 3 = 17
but when programmed ans is 19 yyyyyyy..........???
nt only in this case in many cases it happens i want to know the priorities which comes 1st and all ThX in adv....!!

2. It's undefined behavior as the first couple topics here address:
http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=expr

More:
http://www.embedded.com/story/OEG20020625S0041

3. The order that b++ and --b are performed in relation to the other operations in the expression is undefined, so there is no gurantee that the compiler won't do 3 *3 first, then the --b operation. In particular, using two different increment/decrement operators in the same line of code is not defined behaviour for C and C++.

The general rule is "avoid using increment and decrement operators" in complex expressions, because you can't know for sure what happens. Note that it may well calculate the right result on another compiler, and come up with a different incorrect result on a third compiler.

It is also better for the compiler to do something like this:
Code:
`a = 2*b + (b-1) + 3*(b-1) +b`
because the compiler can easily calculate all those +1 and -1 operations into one caculatlation, so it becomes:
a = 2 *b + b + 3 * b - 4 + b;
We can then optimize this a bit further:
a = 7 * b - 4
And we get the correct result on ANY compiler.

--
Mats

4. And don't use caps.

Code:
```#include<stdio.h>
main()
{
int a,b,s;
printf("Enter the value of s\n");
scanf("&#37;d",&s);
b=s;
a=2*b+(--b)+(3*(b++))+b;
printf("The value of a is %d\n",a);
}```
manually ans i m getting is : when b = 3
a = (2*3) + (2) + ( 3 * 2) + 3 = 17

when programmed ans m gettin is 14
& sometimes 19 pl chk

6. Did you read anything we just wrote? It's undefined behavior!

7. oh k thX matsp

act i was posting tht program bfore i read ny thng posted by u ppl ThX

8. Main returns int.

9. then pl tell me how to solve this : using c program....!!

Code:
`a=2*b+(--b)+3*(b++)+b`

10. What do you want to "solve". I just wrote a different version that is definitive in the second post. Just avoid using ++ and -- in the expression.

--
Mats

11. You really are clueless, aren't you? Even though we told you.
Do not use increment or decrement operators in complex expressions!
--n and n-- just decrements the value by one and ++n and n++ just increments the value by one, so rewrite it!

a = 2 * b + (b - 1) + 3 * (b + 1) + b

12. yes now i got the Logic thX a lot matsp & Elysia

13. Originally Posted by Elysia
a = 2 * b + (b - 1) + 3 * (b + 1) + b
That is incorrect. I posted the correct variant in post #3. Note that the third reference to b is "b++", so it takes "b-1" derived from --b, then restores b's value.
I also noted that we can just as well write:
a = 7*b - 4

--
Mats

14. can ny1 xplain in detail step by step plz....!!

15. is that we replace n- - or - -n by n-1
replace n++ or ++n by n+1

Popular pages Recent additions