Questions tagged [stringification]

Stringification refers to the use of the `#` operator in a **C** preprocessor macro to use an argument as a string. Similarly the preprocessor operator `##` can be used to concatenate two arguments, and this tag can also be used.

Stringification refers to the use of the # operator in a C preprocessor macro to use an argument as a string constant. Similarly the preprocessor operator ## can be used to concatenate two arguments, and this tag can also be used.

For full details consult the manual pages:

Related Tags

174 questions
10
votes
4 answers

CPP: avoiding macro expansion of a macro function parameter

what I'd like to do (for logging purposes) is something like this: This code has been written to show my problem, actual code is complex and yes, I have good reasons to use macros even on C++ =) # define LIB_SOME 1 # define LIB_OTHER 2 # define…
royconejo
  • 2,213
  • 3
  • 24
  • 29
9
votes
2 answers

Stringify Macro with Unicode String Literal

I'm using this preprocessor macro to "stringify" and return easily from a definition resolving function: #define STRINGIFY_RETURN(x) case x: return #x "" It works like a charm in MBSC environment with normal string literals. Example: #define…
Vinz
  • 3,030
  • 4
  • 31
  • 52
8
votes
4 answers

C/C++ macro expanding to argument, argument as string

I have many variables that are named the same as elements in an engineering specification document so the string version of the name is also useful. I find myself using a macro like this a lot: #define MACRO(a) a, #a Typical usage is: void…
paperjam
  • 8,321
  • 12
  • 53
  • 79
8
votes
3 answers

Stringify macro with GNU gfortran

How can I stringify a preprocessor macro with GNU gfortran? I would like to pass a macro definition to GNU gfortran which will then be used as a string in the code. Effectively I would like to do this: program test implicit none character (len=:),…
8
votes
2 answers

C preprocessor: stringize macro and identity macro

I want to know the reason behind the output of this code. I couldn't come up with an answer. #define f(a,b) a##b #define g(a) #a #define h(a) g(a) void main() { printf("%s %s",h(f(1,2)),g(f(1,2))); } PS: output is 12 f(1,2). I thought it was 12…
Vindhya G
  • 1,339
  • 2
  • 21
  • 46
7
votes
2 answers

How do I turn a macro into a string using cpp?

GNU's cpp allows you to turn macro parameters into strings like so #define STR(x) #x Then, STR(hi) is substituted with "hi" But how do you turn a macro (not a macro parameter) into a string? Say I have a macro CONSTANT with some value e.g. #define…
Sean Seefried
  • 1,249
  • 9
  • 18
7
votes
7 answers

What is the # for when formatting using %s

I came across this example of an assertion and was wondering what the # is for: #define ASSERT( x ) if ( !( x ) ) { \ int *p = NULL; \ DBGPRINTF("Assert failed: [%s]\r\n Halting.", #x); \ *p=1; \ }
Fructose
  • 145
  • 5
7
votes
2 answers

Getting JSON data of JSTree, and it's metadata

We're using jstree for a navigation menu editor, and have been assigning metadata to the nodes of the tree like this: var data = currentNode.data("jstree"); data.title = textBoxTitle.val(); data.linkType = textBoxLink.val(); I can see that the data…
Greg
  • 21,235
  • 17
  • 84
  • 107
7
votes
5 answers

Why pre-processor gives a space?

I want to comment a line using the pre-processor: #define open /##* #define close */ main() { open commented line close } when I do $gcc -E filename.c I expected /* commented line */ but I got / * commented line */ so that the…
abubacker
  • 4,638
  • 6
  • 32
  • 35
7
votes
3 answers

How to convert concatenated strings to wide-char with the C preprocessor?

I am working on a project where I have many constant strings formed by concatenation (numbers, etc.). For example, I have a LOCATION macro that formats __FILE__ and __LINE__ into a string that I can use to know where I am in the code, when printing…
7
votes
1 answer

Stringize operator failure

The C and C++ standards all include text to the effect that if a stringize operation fails to produce a valid string literal token, the behavior is undefined. In C++11 this is actually possible, by including a newline character in a raw string…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
6
votes
3 answers

How to add a modifier to a quoted regular (qr) expression

Is there an easy way to add regex modifiers such as 'i' to a quoted regular expression? For example: $pat = qr/F(o+)B(a+)r/; $newpat = $pat . 'i'; # This doesn't work The only way I can think of is to print "$pat\n" and get back…
dividebyzero
  • 1,243
  • 2
  • 9
  • 17
6
votes
2 answers

What does Any.match do?

It has the deceivingly simple code: method match(Any:U: |) { self.Str; nqp::getlexcaller('$/') = Nil } However, this is the behavior it has: (^3).match(1) # OUTPUT: «「1」␤» So far, so good. say (1,3 ... * ).match(77); # OUTPUT: «Nil␤» Ooookey.…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
6
votes
1 answer

Preprocessor macro GCC: pasting x and x does not give a valid preprocessing token

#define PATH "yagh/headers/" #define FILNAME "includefile" #define CONCAT(a__, b__) CONCAT_DO(a__, b__) #define CONCAT_DO(a__, b__) a__##b__ #define CONCATTHREE(a__, b__, c__) CONCAT(CONCAT(a__, b__), c__) #define STRINGIFY(a__)…
6
votes
3 answers

Token pasting in C

After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns…
Nyan
  • 2,360
  • 3
  • 25
  • 38
1 2
3
11 12