This question already has answers here:
C macros and use of arguments in parentheses
(2 answers)
Closed 3 years ago.
I am trying to write this code in code blocks C editor but the output is coming 36 instead of 1.
#include <stdio.h>
#define square(x) x*x
int main()
{
int p=36/square(6);
printf("%d",p);
}
Why is the output 36 instead of 1?
The problem is that the definition of your macro doesn't have any parentheses in it - and macros aren't 'called', like functions are - they just cause text to be replaced.
So, in your code, the line:
int p=36/square(6);
is replaced with:
int p=36/6*6;
which is evaluated in left-to-right order (as the * and / operators have equal precedence), giving, effectively:
int p = (36/6) * 6;
To fix this, add parentheses in your macro definition:
#define square(x) ((x)*(x))
Feel free to ask for further clarification and/or explanation.
The C preprocessor changes the line
int p=36/square(6);
into
int p=36/6*6;
You will get the desired output if your change your preprocesor macro to:
#define square(x) (x*x)
That way, the line will instead be changed to:
int p=36/(6*6);
However, this solution still is not ideal: If you for example write
int p=36/square(6+3);
then the preprocessor will change this to
int p=36/(6+3*6+3);
Therefore, the proper solution would be to change the preprocessor macro to:
#define square(x) ((x)*(x))
That way, the preprocessor will correctly change it to:
int p=36/((6+3)*(6+3));
In this respect, C preprocessor macros behaves very differently from function calls, where the parentheses are not necessary.
Related
This question already has answers here:
Invoke function instead of macro in C
(3 answers)
Why surround the function with parentheses?
(1 answer)
What do the parentheses around a function name mean?
(3 answers)
Closed 4 years ago.
I stumbled over a weird piece of code that I thought would not work the way it is intended, but it did. Example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%d\n", abs(-1)); // output: 1
printf("%d\n", (abs)(-1)); // output: 1
return 0;
}
Apparently putting parenthesis around the function name in a function call has no effect on the program. It just makes it look like a cast.
I am kinda interested if this is defined somehow. But I figure that it just not forbidden and that's why it works.
What I am really curious about is, is there a case where this kind of notation might yield an advantage in any kind? (structure of the code, readability, anything this might be useful for)
Or is it just a "weird" way to write code?
Try
int foo(int a, int b) { return a+b; }
#define foo(a,b) ((a)+(b)+1)
#include <stdio.h>
int main() {
printf("%d %d\n",
foo(3, 5), // Use macro if defined
(foo)(3, 5) // Always call function even if there's a macro
);
return 0;
}
Output:
9 8
See the difference?
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:
#define IDENTIFIER without a token
(4 answers)
Closed 8 years ago.
First, it declares the macro LOG_DBG(x) just like this:
#define LOG_DBG(x)
And then, use it like this:
LOG_DBG((LOG_MISC, 80, __FUNCTION__": select reports %d", res));
And I can't understand what the macro means ? What it will do ?
It does exactly nothing. It gets preprocessed to an empty string. This may be a somewhat convenient way of disabling/enabling debug log messages. The code was probably written like this at some point:
#define LOG_DBG(x) some_logging_function(x)
But then someone wanted to simply get rid of all the log messages in one shot.
It will do nothing. You're seeing the "disabled" form of the macro, which is used to make the logging calls go away on non-debug builds.
There should be another declaration of the macro somewhere, or the code is perhaps just "left" in the non-debugging state.
You've defined a macro that receives x as parameter but do nothing. Here's a concrete use of macro:
#include <stdio.h>
#define LOG_DBG(x) ((x)+5)
int main(int argc, char *argv[])
{
int x = LOG_DBG(3);
printf("%d\n", x);
return 0;
}
Here, we did something in the macro and hence the number 8 will be printed.
Of course, if you want the logarithm of x, it would be another 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.