some very simple increment/decremant operator question

R

rahul8143

hello,
I am confused with following programs snippet code about how the
answer comes? statement/expression is evaluated?

1) int i=5;
i=i++*i++*i++*i++;
why the answer is not 5*6*7*8= 1680 but its 629?

2) int val=2;
val = - --val- val--- --val;
how to evaluate this?

3) int i=5;
printf("%d %d %d %d %d\n",++i,i++,i++,i++,++i);
why ans is not 6 6 7 7 10 but its 7 6 6 6 6?
regards,
rahul.
 
E

Eric Sosman

hello,
I am confused with following programs snippet code about how the
answer comes? statement/expression is evaluated?

1) int i=5;
i=i++*i++*i++*i++;
why the answer is not 5*6*7*8= 1680 but its 629?

2) int val=2;
val = - --val- val--- --val;
how to evaluate this?

3) int i=5;
printf("%d %d %d %d %d\n",++i,i++,i++,i++,++i);
why ans is not 6 6 7 7 10 but its 7 6 6 6 6?

See Questions 3.2 and 3.3 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
 
J

john_bode

hello,
I am confused with following programs snippet code about how the
answer comes? statement/expression is evaluated?

1) int i=5;
i=i++*i++*i++*i++;
why the answer is not 5*6*7*8= 1680 but its 629?

2) int val=2;
val = - --val- val--- --val;
how to evaluate this?

3) int i=5;
printf("%d %d %d %d %d\n",++i,i++,i++,i++,++i);
why ans is not 6 6 7 7 10 but its 7 6 6 6 6?
regards,
rahul.

I should just direct you to the FAQ but I'm feeling generous today.
The short answer is that the result of any expression of the form
i=i++, i++*i++, etc., is undefined.

The long answer involves explaining how the autoincrement/decrement
operators actually work. The expression "i++" evaluates to the current
value of i; as a side affect, i is incremented by one, but exactly
*when* the side affect is applied isn't specified other than it must
happen before the next sequence point. A sequence point may be the end
of a statement, one of the Boolean operators && or ||, and (I think) a
comma operator (which is not the same thing as a comma separator in an
argument list).

So, given the statement

i = j++ * k++;

i is assigned the *current* value of j multiplied by the *current*
value of k; and sometime before the next sequence point, j and k are
incremented. The increments may happen as each expression is evaluted,
or after both j and k are evaluated but before the result is assigned
to i, or after the assignment to i. Each of the following are valid:

1. Update each object as it is evaluated; this is how most people
expect autoincrement/decrement to work, and it may actually work this
way occasionally:

t1 <- j
j <- j + 1
t2 <- k
k <- k + 1
i <- t1 * t2

2. Update objects after all expressions have been evaluated but before
assignment:

t1 <- j
t2 <- k
j <- j + 1
k <- k + 1
i <- t1 * t2

3. Update objects after all expressions have been evaluated and the
assignment has been carried out:

i <- j * k
j <- j + 1
k <- k + 1

The exact order depends on the compiler, the optimization settings, the
surrounding code, etc.

So basically, none of the expressions you provide should be expected to
yield correct results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top