Why am I receiving different outputs when I run this code on visual studio and gcc (unix)?. The correct output is -52.
#define polyMacro(a, b) ((a * a) + (8 * a) + (3 * a * b) - (b * b))
int polyFunc(int a, int b) {
return ((a * a) + (8 * a) + (3 * a * b) - (b *
b));
}
int x = -7, y = 3;
int x_copy = x, y_copy = y;
printf(" polyFunc(x, y) = %d \n polyMacro(x, y)
= %d \n\n", polyFunc(--x, --y), polyMacro(x, y));
VS output:
polyFunc(x, y) = -52
polyMacro(x, y) = -52
gcc output:
polyFunc(x, y) = -52
polyMacro(x, y) = -79
// do comment if any problem arises
//This is given code
#include <stdio.h>
#define polyMacro(a, b) ((a * a) + (8 * a) + (3 * a * b) - (b * b))
int polyFunc(int a, int b)
{
return ((a * a) + (8 * a) + (3 * a * b) - (b * b));
}
int main()
{
int x = -7, y = 3;
int x_copy = x, y_copy = y;
printf(" polyFunc(x, y) = %d \n polyMacro(x, y) = %d \n\n", polyFunc(--x, --y), polyMacro(-7, 3));
}
Yes Question is correct,
Output in visual studio:
Output in gcc:
This problem arises due to use of undefined constructs in given program because it is totally compiler dependent,
if it processes macro before execution of program or during program execution.
According to output gcc is processing macros before execution of program that's why value of x and y doing to macro is -7 and 3 itself without any decrements.
on the other hand visual studio first decrements x and y then processes macro.
So, none is the correct answer to this program, the best answer is undefined behavior.
Get Answers For Free
Most questions answered within 1 hours.