-3

I've written a C program and then compiled and run it in MS Visual Studio and then using GCC. The program makes a few simple math calculations. But the outputs/results I get from both are different. The program is based on macros.

Do these programming environments have a different way of handling macros? If so, what is the difference?

EDIT: Sorry, here's the code.

#include <stdio.h>
#define mac(a,b) a*a + b*b - 2*a*b

int func(int a, int b) {
    return (a*a + b*b - 2*a*b);
}
main() {
    int f, g, i, j, x, y;
    printf("Please enter two integers\n");
    scanf("%d%d", &f, &g);
    printf("f = %d\tg = %d\n", f, g);
    i = f;
    j = g;
    x = func(i, j);
    y = mac(i, j);
    printf("x = %d\ty = %d\n", x, y);
    x = func(++i, ++j);
    i = f;
    j = g;
    y = mac(++i, ++j);
    printf("i = %d\tj = %d\n", i, j);
    printf("x = %d\ty = %d\n", x, y);
}

Here's the output using VS:

f = 7       g = 8 
x = 1     y = 1 
i = 10    j = 11 
x = 1   y = 1

And using GCC:

f = 7   g = 8 
x = 1   y = 1 
i = 10 j = 11 
x = 1  y = -39

The difference is the last y value. So I'm wondering if the different compilers go through the macro's process differently?

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
aclark
  • 219
  • 1
  • 4
  • 13
  • 10
    Show us some code. You probably have some sort of undefined behavior such as `x = x++ + ++x;` – Mysticial Jan 24 '12 at 18:16
  • 5
    How different are the results? If one says "3.14159266666666" and the other says "3.14159266666667" you should read ["what every computer scientist should know about floating point"](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – pmg Jan 24 '12 at 18:17
  • 3
    To attempt to directly answer your question: They try not to. – Drew Dormann Jan 24 '12 at 18:20
  • You may also be having problems with the difference between 32 and 64 bits platforms. Indeed, you should show us the code (or at least the function where the difference happens). – Renan Greinert Jan 24 '12 at 18:58

1 Answers1

7
y = mac(++i, ++j);

expands to

y = (++i * ++i) + (++j * ++j) - (2 * ++i * ++j)

Is this really what you had in mind? I imagine you wanted to evaluate the square of (a - b), so you should use:

++i; 
++y;
y = mac(i,j)

Your problem is that the order of evaluation is compiler-dependant. You should never call macros with parameters that have side-effects (like ++) as they may (as in your case) evaluate multiple times. Also your macro should really be written with more brackets, at a minimum like this:

#define mac(a,b) (a)*(a) + (b)*(b) - 2*(a)*(b)

which protects you against calls like

y = mac(i+1, j+1);

which in your example would expand to

y = i+1*i+1 + j+1*j+1 - 2*i+1*j+1

and hence would not evaluate correctly.

Try using the -E option to gcc to check the pre-processor output for macros if in doubt (ie after headers and macros have been expanded but before compilation). It will generate a huge amount of text, but you will be able to find your macro expansion towards the bottom. For example:

gcc -E file.c -o file.txt
William Morris
  • 3,554
  • 2
  • 23
  • 24