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.
Related
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 24 days ago.
while reading about the c preprocessor I got something like array[x=y,x+1]. I haven't seen this kind of syntax in c before and after searching for many hours I didn't find anything useful.
#include <stdio.h>
int main() {
int arr[] = {5,10,15};
printf("%d %d %d",arr[0,1]);
return 0;
}
outputs:
10 1762365112 1769491896
Can someone elaborate on this?
Because of how the comma operator (,) works in C, the effect of the (questionable and woefully hard to read) array[x=y,x+1] is to first copy the value of y into x and then use x+1 as the normal (single dimensional, non-ranged, or whatever you were expecting) index in to the array.
(I do not see the need to discuss your experimental code.)
See the link to documentation of comma operator, as kindly provided in comments by Pignotto:
https://en.cppreference.com/w/c/language/operator_other#Comma_operator
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.
This question already has answers here:
What does this do? [duplicate]
(4 answers)
Closed 8 years ago.
I got a line in the generic code of stm32f0 so often and I can't get it clearly. what does it mean by the line below. I know its complicated to understand this way. But my point is the question mark(?) in the define. Can somebody explain that.
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
it is a short form of condition,
you can write
int k = (i > j) ? i : j;
this will asign i to k if i > j else it will asign j.
this is the way we choose between two options inside a macro
Here a conditional operator is used .
It says if expr is true then the value of the Macro assert_param(expr) will be zero or otherwise
it will call another macro or function assert_failed() with two parameters as input .
1st is FILE macro which is the filename from which the macro has been called and another is LINE
macro which is the line number in the file from where the macro has been called.
The conditional operator format is like this:
a?b:c
It means if a is true then b will be the result otherwise c will be the result.
Accordingly you can separate these statements in the expression.
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.
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.