# How Does Increment Operators Work...??

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 02-06-2008
ajayd
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..........???:confused:
nt only in this case in many cases it happens i want to know the priorities which comes 1st and all ThX in adv....!!
• 02-06-2008
hk_mp5kpdw
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
• 02-06-2008
matsp
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
• 02-06-2008
Elysia
And don't use caps.
• 02-06-2008
ajayd

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
• 02-06-2008
hk_mp5kpdw
Did you read anything we just wrote? It's undefined behavior!
• 02-06-2008
ajayd
oh k thX matsp

act i was posting tht program bfore i read ny thng posted by u ppl ThX
• 02-06-2008
Elysia
Main returns int.
• 02-06-2008
ajayd
then pl tell me how to solve this : using c program....!!

Code:

a=2*b+(--b)+3*(b++)+b
• 02-06-2008
matsp
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
• 02-06-2008
Elysia
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
• 02-06-2008
ajayd
yes now i got the Logic thX a lot matsp & Elysia :)
• 02-06-2008
matsp
Quote:

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
• 02-06-2008
ajayd
can ny1 xplain in detail step by step plz....!!
• 02-06-2008
ajayd
is that we replace n- - or - -n by n-1
replace n++ or ++n by n+1
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last