Please Explain This C Code Snippet Mult(X, Y)X*Y?
#Include #Define Mult(X, Y)X*Y /* What Does This Mean? ? */ Int Main() { Int A, B, Answer; B=5; A=5; Answer=Mult(A+B, A+B); Printf("%D", Answer); Return 0; }...
#include <stdio.h>
#define mult(x,y)x*y /* what does this mean ?? */
int main()
{
int a,b,answer;
b=5;
a=5;
answer=mult(a+b,a+b);
printf("%d",answer);
return 0;
}
I'm using compiler gcc-4.9.2
1 Answer
If I understand correctly you want to know what #define mult(x,y) x*y does.
This a definition of a macro, during the compilation the compiler will replace, everywhere in the code, mult(x, y) by x*y.
In your code:
answer=mult(a+b,a+b);
Will be replaced by: answer=a+b*a+b;
Whence the answer will be 35.
A correct way to use macros and to ensure they work properly is to include parentheses where they might be needed.
Therefore your define would be: #define mult(x, y) ((x)*(y)) to ensure the result is what you would expect 100