Combination of Token pasting and stringizing [duplicate] - c

This question already has answers here:
# and ## in macros
(3 answers)
Closed 9 years ago.
Following is the code including Combination of Token pasting and Stringizing as follows:
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
The output is:
12
f(1,2)
I do not uderstand the difference in ordering in the two expressions.

When you use the # expression in the preprocessor, it takes the incoming argument and makes a string of it.
In your code:
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
when you call h(f(1,2)), it translates to g(f(1,2)), which in turn is a macro, so the macro expander parses that, starting from the inside with f(1,2), which is 12, then passes that as g(12), which results in "12".
With g(f(1,2)), f(1,2) is used directly, because there is no expansion of with #a, it just uses whatever is passed in as a.

Related

Macro argument with different results [duplicate]

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.

How to embed an integer in a macro string? [duplicate]

This question already has answers here:
Stringification - how does it work?
(2 answers)
Closed 7 years ago.
My first time seeing stringification and token pasting. I feel like it could be a good tool for this macro:
#define MY_NUMBER 3
#define MY_STRING "the number three: ##MY_NUMBER"
printf("%s\n", MY_STRING);
should output:
the number three: 3
try this
#define S_(x) #x
#define S(x) S_(x)
#define MY_NUMBER 3
#define MY_STRING "the number three: " S(MY_NUMBER)

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

What does ## mean in the #define directive in the code here [duplicate]

This question already has answers here:
What does ## in a #define mean?
(6 answers)
Closed 2 months ago.
Please tell me the answer with explanation:
#define f(g,h) g##h
main(){
printf("%d",f(100,10));
}
## is used to concatenate whatever is before the ## with whatever is after it. It is used for concatenation.
You can check the reference for details
A ## operator between any two successive identifiers in the
replacement-list runs parameter replacement on the two identifiers
(which are not macro-expanded first) and then concatenates the result.
This operation is called "concatenation" or "token pasting".
Here is a useful duplicate - What does ## in a #define mean?
Example
For
//Definition
#define ArgArg(x, y) x##y
//Example
ArgArg(lady, bug) -> "ladybug"

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