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
433
votes
12 answers

How do you convert a jQuery object into a string?

How do you convert a jQuery object into a string?
chief7
  • 14,263
  • 14
  • 47
  • 80
118
votes
4 answers

Pragma in define macro

Is there some way to embed pragma statement in macro with other statements? I am trying to achieve something like: #define DEFINE_DELETE_OBJECT(type) \ void delete_ ## type_(int handle); \ void delete_…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
110
votes
3 answers

Concatenate int to string using C Preprocessor

I'm trying to figure out how I can concatenate a #define'd int to a #define'd string using the C Preprocessor. My compiler is GCC 4.1 on CentOS 5. The solution should also work for MinGW. I'd like to append a version number onto a string, but the…
jonescb
  • 22,013
  • 7
  • 46
  • 42
93
votes
2 answers

How, exactly, does the double-stringize trick work?

At least some C preprocessors let you stringize the value of a macro, rather than its name, by passing it through one function-like macro to another that stringizes it: #define STR1(x) #x #define STR2(x) STR1(x) #define THE_ANSWER 42 #define…
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
90
votes
13 answers

What are the applications of the ## preprocessor operator and gotchas to consider?

As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor…
John Rudy
  • 37,282
  • 14
  • 64
  • 100
83
votes
6 answers

Convert a preprocessor token to a string

I'm looking for a way to convert a preprocessor token to a string. Specifically, I've somewhere got: #define MAX_LEN 16 and I want to use it to prevent buffer overrun: char val[MAX_LEN+1]; // room for \0 sscanf(buf, "%"MAX_LEN"s", val); I'm open…
davenpcj
  • 12,508
  • 5
  • 40
  • 37
61
votes
2 answers

Stringification - how does it work?

I know that: #define foo 4 #define str(s) #s with str(foo) writes out: "foo", because stringify is executed first of text expansion, but this: #define xstr(s) str(s) #define str(s) #s #define foo 4 with xstr(foo) writes out: "4". Why? What…
Marco A.
  • 43,032
  • 26
  • 132
  • 246
57
votes
3 answers

# and ## in macros

#include #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; } Just by looking at the program one "might" expect the output to be,…
algo-geeks
  • 5,280
  • 10
  • 42
  • 54
57
votes
4 answers

What does #x inside a C macro mean?

For example I have a macro: #define PRINT(int) printf(#int "%d\n",int) I kinda know what is the result. But how come #int repersent the whole thing? I kinda forget this detail. Can anybody kindely give me a hint? Thanks!
Anders Lind
  • 4,542
  • 8
  • 46
  • 59
52
votes
2 answers

C Preprocessor, Stringify the result of a macro

I want to stringify the result of a macro expansion. I've tried with the following: #define QUOTE(str) #str #define TEST thisisatest #define TESTE QUOTE(TEST) And TESTE gets expanded to: "TEST", while I'm trying to get "thisisatest". I know this is…
Ale Morales
  • 2,728
  • 4
  • 29
  • 42
48
votes
3 answers

Opposite of C preprocessor "stringification"

When using C preprocessor one can stringify macro argument like this: #define TO_STRING(x) "a string with " #x and so when used, the result is as follows: TO_STRING(test) will expand to: "a string with test" Is there any way to do the opposite? Get…
MasterM
  • 1,173
  • 1
  • 9
  • 15
48
votes
8 answers

Stringify template arguments

Is it possible in C++ to stringify template arguments? I tried this: #include #define STRINGIFY(x) #x template struct Stringify { Stringify() { std::cout << STRINGIFY(T) << endl; } }; int main()…
sold
  • 2,041
  • 5
  • 25
  • 32
33
votes
7 answers

Macros to create strings in C

Alternative titles (to aid search) Convert a preprocessor token to a string How can I make a char string from a C macro's value? Original Question I would like to use C #define to build literal strings at compile time. The string are domains…
Richard Stelling
  • 25,607
  • 27
  • 108
  • 188
30
votes
25 answers

What are some tricks I can use with macros?

In our legacy code, as well as our modern code, we use macros to perform nifty solutions like code generations, etc. And we make use of both the # and ## operators. I am curious how other developers use macros to do cool things, if they use them at…
Sasha
30
votes
7 answers

Why can't you stringify a function expression?

Why doesn't this produce anything? console.log(JSON.stringify(function(){console.log('foobar');}));
wwaawaw
  • 6,867
  • 9
  • 32
  • 42
1
2 3
11 12