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

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)

Related

How to add L prefix to MACRO [duplicate]

This question already has an answer here:
C preprocessor Literal Construction
(1 answer)
Closed 6 years ago.
I have
#define STRING "string"
I want to add L prefix to STRING macro later in my code (I can't do it in definition). How to do it?
This should do:
#include <stdio.h>
#define CONCATENATE(e1, e2) e1 ## e2
#define PREFIX_L(s) CONCATENATE(L, s)
#define STRING "string"
int main(void)
{
printf("%ls\n", PREFIX_L(STRING));
}
These standard C pre-processor's concatenation capabilities are also covered by the documentation coming with GCC: https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html
This should work.
#define STRING(x) x"string"
char * l_pString = STRING("");
wchar_t * l_pWideString = STRING(L);

C Preprocessor Instructions (SQR-Funktion) [duplicate]

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 6 years ago.
Want to know how "11" is the answer of this c preprocessor instruction:
#define SQR(x) x*x
int main()
{
SQR(2+3);
}
Try expanding the macro manually.
It will be 2+3*2+3 and this is evaluated as 11.

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"

Combination of Token pasting and stringizing [duplicate]

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.

Resources