0

I am practicing for my exam in C, and I have problem with this code. It's clear to me that in first printf program displays first unchanged values of x and y. But when we call DO, variable x should change value and accept values of b because of this (a=b), and finally b should have value of this b=(25)-15 and finally b=10. But my program displays 15, 15 instead of 15,10. Can some good soul explain me what I am doing wrong here ?

#define DO(a,b) b=(a+b)-(a=b)
int main (void)
{
    int x = 10;
    int y = 15;
    printf ("%d %d\n",x,y);
    DO(x,y);
    printf ("%d %d\n",x,y);
    return 0;
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    Is this a typo? `(a=b)`. Did you mean `(a-b)` instead? – devnull Feb 24 '14 at 12:07
  • 2
    If that isn't a typo (and even if it *is*), [**you may want to read this**](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – WhozCraig Feb 24 '14 at 12:07
  • In such case, C language doe not specify which assignment is executed first whether assignment to a or b. Your compiler have chosen other evaluation order as you supposed. – Marian Feb 24 '14 at 12:09
  • 1
    Strange exams you have that rely upon undefined behavior to produce a given result. – devnull Feb 24 '14 at 12:10
  • ... and that seemingly unnatural compiler choice you *cannot* rely on. Seriously read that linked question and answers. – WhozCraig Feb 24 '14 at 12:10
  • may be you are looking for http://stackoverflow.com/questions/20800684/why-is-a-ab-b-a-a-bad-choice-for-swapping-two-integers – kevin gomes Feb 24 '14 at 13:44

3 Answers3

2

b=(a+b)-(a=b)

You do two assignments in the same line, it's usually Undefined Behavior.

Didn't you mean

b=(a+b)-(a-b)

?

More information on Sequence Points and why what you wrote is UB can be found here (thanks Axel!)

Community
  • 1
  • 1
Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • I just wanted to say the same thing. So I'll add the link to a question where this is explained in detail as a comment: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Axel Feb 24 '14 at 12:10
2

Your DO macro will expand to:

y = (x+y) - (x=y);

You have no guarantee as to which of those brackets will be evaluated first.

If (x=y) is evaluate first, it will evaluate to 15 (and set x to 15), so the whole thing will evaluate to

y = (15+15) - 15;

so y will be set to 15, and print 15, 15

If x+y is evaluated first, the expression will evaluate as:

y = (10+15) - 15;

And y will equal to, so it will print 15, 10.

The behaviour is thus undefined, as you do not have a sequence point which would ensure the order of evaluation. For more details on sequence points (on C++, but C is the same), look here: Undefined behavior and sequence points

Community
  • 1
  • 1
abligh
  • 24,573
  • 4
  • 47
  • 84
1

From your description seems like you are trying to swap variable with macro DO

Use below macro to swap numbers..

#define DO(a,b){ a = a + b; b = a - b; a = a - b; }
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31