Macro argument with different results [duplicate] - c

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 7 years ago.
So I have been having this rather 'unyielding' problem with a macro call in my C program.
The macro I used was :
#define MACRO(X) X*X
And the problem is when I do this
printf("%d",MACRO(3));
it displays 9 as a result (which is correct). Only when I pass 3 as 2+1 as follows :
printf("%d",MACRO(2+1));
It strangely displays an outcome of 5.
Can anyone please have me informed why?

printf("%d",MACRO(2+1));
After preprocessing it will be become
printf("%d",2+1*2+1);
Since multiplication has high precidenece over addition, it will print 5;
To resolve the issue you have to define your macro as follows
#define MACRO(X) (X)*(X)

You need to define your macro like so
#define MACRO(X) (X)*(X)
A macro is just a text expansion, so your following code is being interpreted like this...
printf("%d", 2+1*2+1);
And following order of operations you can see how you get 5 as your result.

Related

What is happening in this C code? (From interview) [duplicate]

This question already has answers here:
Square of a number being defined using #define
(11 answers)
Closed 5 years ago.
I was given a variation of this C code in an interview recently, and asked what would be returned by the function.
#define fun(a) a*a
int main() {
return(fun(4+5));
}
I've run it with a printf("%d"...) in place of the return and it prints 29. No other types than "%d" showed a result. Can someone explain what is going on here?
a is expanded, not evaluated as a. So you get:
4+5*4+5
and with operator priorities; you get 4 + 20 + 5 => 29
better way (with extra outside parenthesis for protecting against outside operators too thanks to Thomas comment):
#define fun(a) ((a)*(a))
but all parenthesis of the world still don't protect against i++, function calls (with side effects, preferably)... so argument reusing in macros is always a problem (there's no syntax to declare & assign a temp variable either). Prefer inlined functions in that case.

what is the output of this according to the macro definition [duplicate]

This question already has answers here:
Strange behavior of Macro-expansion
(3 answers)
Why is the return value not what I expected in this C program with macro? [duplicate]
(2 answers)
Closed 7 years ago.
#define A 1+2
#define B 4+3
int main()
{
int val = A*B;
printf("%d",val);
}
Here A and B is defined, and val is (A*B). What is the correct output?
Let's trace the expansion and calculation manually.
A*B -> 1+2*4+3 -> 1+8+3 -> 12
As a result, the output will be 12
This is a common issue with Macros. When you define something even as x+y in a macro it is advisable to first rap them in () so as to "protect" the operation.
As such it would be better to define A as (1+2) etc. Otherwise you get the output 1+2*4+3=12 as people have stated above.

Why this code is give error? [duplicate]

This question already has an answer here:
Macro concatenation using compiler define
(1 answer)
Closed 7 years ago.
#define cat(x,y) x##y
main()
{
printf("%d",cat(cat(cat(3,3),cat(3,4)),5));
}
Why this is give error?
As per my knowledge O/P should be
cat(cat(3,3),cat(3,4)),5
because due to concatenation inner macro is not expanding. Why this is not happening?
call by indirect.
#define cat_(x,y) x##y
#define cat(x,y) cat_(x,y)
If more than one ## operator and/or # operator appears in the replacement list of a macro definition, the order of evaluation of the operators is not defined.
Read more here

Why is the return value not what I expected in this C program with macro? [duplicate]

This question already has answers here:
Strange behaviour of macros C/C++
(5 answers)
Strange behavior of Macro-expansion
(3 answers)
Closed 9 years ago.
When I run the following code, the return value is 11, but I was expecting it to return 25. Can someone explain this?
#include<stdio.h>
#define SQR(a) a*a
int main()
{
int i=3;
printf("%d",SQR(i+2));
return 1;
}
Needs more parentheses. This:
#define SQR(a) a*a
expands to this:
i+2*i+2
which is:
3+2*3+2
which is 11 because * has precedence over +.
You need to define your macro like this:
#define SQR(a) ((a)*(a))
to ensure that this kind of thing doesn't happen.
Macros are not the same as regular functions.
During the reprocessing all macros are replaced exactly by what they are define. In your case, the line:
printf("%d",SQR(i+2));
is replaced by the line:
printf("%d", i+2*i+2);
So, you see the unexpected result there.
The correct way is:
#define SQR(a) ((a)*(a))
The preprocessor result will be:
printf("%d", ((i+2)*(i+2)));
Try to learn on this mistake. Issues of this kind are quite hard to debug.

Why i am not getting the expected output in the following c programme? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “#define STR(a) #a” do?
Macros evaluation in c programming language
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}
I was expecting the output to be same for both the printf. But what I am getting is different(given below)
12
f(1,2)
can someone explain what is the reason and why is it happening in detail?
I extended your program with an additional line
printf("%d\n",f(1,2));
which, in turn, results into
printf("%d\n",12);
(called with gcc -E).
Your two lines result into
printf("%s\n","12");
printf("%s\n","f(1,2)");
What happens here?
f(1,2) is clear - 1 and 2 just get sticked together.
g(something) just reproduces something as a string, without treating it specially -> "f(1,2)".
h(something), in turn, lets the result of g(something) expand.
C standard states that macro arguments aren't expanded if they are stringified or concatenated. That is why g(YOUR_MACRO) YOUR_MACRO isn't expanded. However in h(YOUR_MACRO) case - h() does stringification indirectly and so it complies with C macro arguments expansion rules and is expanded further.
First one:
printf("%s\n",h(f(1,2)));
becomes:
g(12)
which in turn becomes
"12"
Second one:
printf("%s\n",g(f(1,2)));
becomes
"f(1,2)"
since # converts the argument to a string parameter.

Resources