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
28
votes
5 answers

Preprocessor macro value to Objective-C string literal

I have a preprocessor macro defined in build settings FOO=BAR That value I want to massage into an Objective-C string literal that can be passed to a method. The following #define does not work, but it should demonstrate what I am trying to…
vagrant
  • 868
  • 1
  • 9
  • 23
20
votes
5 answers

Can I split a long #include directive into two lines?

I wish there was a way to split a #include directive across two lines, so that my code can conform to 80 characters per line, despite the necessity of a very long include path. Other than expanding the search path of the compiler, how can I manage…
Jonathan Mayer
  • 1,432
  • 13
  • 17
17
votes
4 answers

Preprocessor tomfoolery (stringifying a #include)

Note: This question has nothing to do with OpenCL per se... check the last paragraph for a succinct statement of my question. But to provide some background: I'm writing some C++ code that makes use of OpenCL. I like to keep the source for my OpenCL…
Dan
  • 528
  • 2
  • 6
  • 14
16
votes
1 answer

Why does `eq` not work when one argument has overloaded stringification?

I have realised (the hard way) that operator eq gives a fatal runtime error when one of the operand is an object with overloaded stringification. Here is a minimal example: my $test = MyTest->new('test'); print 'yes' if $test eq 'test'; package…
scozy
  • 2,511
  • 17
  • 34
14
votes
2 answers

Accessing the value of a Preprocessor Macro definition

If I add a macro "FOO=bar" under GCC_PREPROCESSOR_DEFINITIONS (or Preprocessor Macros if you use XCode"), what would be the best way to access the value of "FOO"? Currently, I use the clumsy: #define MACRO_NAME(f) #f #define MACRO_VALUE(f) …
14
votes
4 answers

What does ## mean for the C(C++) preprocessor?

I have a C program below: #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } when I run just the preprocessor it expands this as { int var12=100; printf("%d",var12); } which is the reason why the output is 100. Can anybody…
Vijay
  • 65,327
  • 90
  • 227
  • 319
13
votes
2 answers

Evaluate preprocessor token before ## concatenation

I would like to evaluate a token before it is concatenated with something else. The "problem" is that the standard specifies the behaviour as before the replacement list is reexamined for more macro names to replace, each instance of a ##…
hlovdal
  • 26,565
  • 10
  • 94
  • 165
13
votes
1 answer

How to cause macro expansion before concatenation?

#define JNI_DECLARE( classname, methodname ) \ classname ## methodname( JNI* env ) #define JAVA_CLASS Java_com_example void JNI_DECLARE( JAVA_CLASS, open ) {} This expands to: void JAVA_CLASS_open( JNI* env ) {} How do I get: void…
user48956
  • 14,850
  • 19
  • 93
  • 154
13
votes
5 answers

How to single-quote an argument in a macro?

I would like to create a C pre-processor macro that will single-quote the argument. Just like the common used #X. I want Q(A) to be expanded to 'A'. I am using gcc on Linux. Does any one have an idea? I know # double-quotes. I am looking for a…
eyalm
  • 3,366
  • 19
  • 21
12
votes
3 answers

Is there a way to use C++ preprocessor stringification on variadic macro arguments?

My guess is the answer to this question is no, but it would be awesome if there was a way. To clarify, assume I have the following macro: #define MY_VARIADIC_MACRO(X...) // Does some stuff here in the macro definition What I would like to do is…
Hazok
  • 5,373
  • 4
  • 38
  • 48
12
votes
3 answers

Error when defining a stringising macro with __VA_ARGS__

I have been trying to implement a function macro in C that prepends "DEBUG: ", to the argument, and passes its arguments to printf: #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) This gives me this error in…
12
votes
4 answers

What is the compiler seeing with this macro?

Consider this: #define STRINGIFY(A) #A If I then later write: STRINGIFY(hello) Is the compiler actually seeing this: #hello I think it is that additional hash in front of #A that is confusing me.
johnbakers
  • 24,158
  • 24
  • 130
  • 258
11
votes
1 answer

Macro substitution in #include directive

I would like to use an #include directive with a file name that is passed as an externally defined macro. E.g. #include #FILE".h" where FILE would be defined as the string MyFile (without quotes), resulting in #include "MyFile.h" The stringizing…
user1196549
10
votes
4 answers

C Macro Token Concatenation involving a variable - is it possible?

I'm trying to define a macro to generate a token name, containing a variable. Basically, what I'm trying is this: #define GLUER(x,y,z) x##y##z #define PxDIR(x) GLUER(P,x,DIR) int main() { int port; port = 2; PxDIR(port) |= 0x01; } I'm hoping…
gatesphere
  • 101
  • 1
  • 1
  • 3
10
votes
2 answers

Stringify first level macro expansion C

Is it possible to stringify this C macro: #define GPIO_INT_PIN (GPIO_PORT_D|GPIO_PIN_IRQ_RISING|GPIO_PIN5) using something like MY_STRINGFY(GPIO_INT_PIN) to get "(GPIO_PORT_D|GPIO_PIN_IRQ_RISING|GPIO_PIN5)" ?
Heeryu
  • 852
  • 10
  • 25
1
2
3
11 12