By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,590 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,590 IT Pros & Developers. It's quick & easy.

increment and decrement problem in c

P: 1
slove this problem

include<stdio.h>
include<conio.h>
void main()
{
int i=1;
printf("%d",i++*++i);
getch();
}
if the ans is 3 explain it how;
there is a problem
it is found that the order of precedence of increment and decrement operator
with respect to other arithmetic operator is not well define.
in some computer the result may vary , it may be 3 and 4 both if we use torbo c++ for compilation. same for printing
printf("%d",i++*++i*++i)
result is expected 18 by RL rule by in my torbo c it gives 12 ans
plz write if u have any rule to sort it out.
Aug 8 '07 #1
Share this Question
Share on Google+
3 Replies


ilikepython
Expert 100+
P: 844
slove this problem

include<stdio.h>
include<conio.h>
void main()
{
int i=1;
printf("%d",i++*++i);
getch();
}
if the ans is 3 explain it how;
there is a problem
it is found that the order of precedence of increment and decrement operator
with respect to other arithmetic operator is not well define.
in some computer the result may vary , it may be 3 and 4 both if we use torbo c++ for compilation. same for printing
printf("%d",i++*++i*++i)
result is expected 18 by RL rule by in my torbo c it gives 12 ans
plz write if u have any rule to sort it out.
The behaivior is undefined whenever you increment a variable more than once. The result can be anything and shouldn't be relied on.
Aug 8 '07 #2

P: n/a
++ operator has higher priority than *...

so in case pintf("%d",i++ * ++i);
the value of i is incremented by 1 in i++ but it is not assigned in i

in case of ++i value of i is incremented by i and it is assigned in i

the initial value of i is 1

try to dry run (i++ * ++i)....in i++ the value of i is incremented...as the value is not assigned the result of i++ operation is 1

but the value which in i becomes 2...after ++i operation the value of i becomes 3

multiply 1 and 3 the result is 3

apply the same logic in the later problem...hope u will get the correct result
Oct 3 '10 #3

Expert 100+
P: 2,398
Look here and here and here and here.
Oct 4 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.