1

How to retrieve the text expanded from a macro?

#include <stdio.h>

#define paste(front, back) front ## back

int main()
{
    int number1 = 23;
    int number2 = 64;

    printf("%d\n", paste(number, 2));


    return 0;
}

The output is: 64 because the macro is expanded as:

printf("%d\n", number2);

How can I print the identifier number2 as string by using the defined paste macro. I need to know how to create the output: number2: 64 by not writing directly "number2" I don't want this solution:

printf("number2: %d\n", paste(number, 2))

I want a dynamic solution. I try to concatenate by doing:

printf("%s: %d\n", paste(number, 2), paste(number, 2));

But it doesn't work because number2 returned by the paste macro is an the identifier an integer How can I retrieve as a text (string)?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PlainOldProgrammer
  • 2,725
  • 5
  • 22
  • 30

2 Answers2

2

Use the stringizing directive #:

#define stringize_impl(x) #x
#define stringize(x) stringize_impl(x)

printf("%s: %d\n", stringize(paste(number, 2)), paste(number, 2));

On a side note: why are you trying to do this? Surely there must be a better solution.

  • The output is *paste(number, 2): 64* The problem is that the paste macro isn't expanded when is used intro stringize but I don't know why. I'm using gcc. I'm doing only for learning purpose (In K&R book explain how to use the # to expand the macro parameter as string). – PlainOldProgrammer Sep 01 '13 at 18:21
  • @Wronski I've tested it locally using clang and using IDEOne with GCC ([link](http://ideone.com/g9QVJQ)). It is guaranteed to work by the standard (here's a [related question](http://stackoverflow.com/questions/2751870/how-exactly-does-the-double-stringize-trick-work)). –  Sep 01 '13 at 18:22
  • @H2CO3 I am guessing Wronski is doing this to see what the macros expand to programmatically. – Morten Jensen Sep 01 '13 at 18:27
  • @H2CO3 Thanks. When I first see the answer I don't see the #define stringize_imp macro. This solution works perfectly but is like 2-step and a little weird but works! – PlainOldProgrammer Sep 01 '13 at 18:29
  • 1
    @Wronski _"a little weird"_ in what sense ? However, your question is.. :P – P0W Sep 01 '13 at 18:32
  • @Wronski Indeed, I had yet to correct it, I assumed you didn't see the first (short-lived) version. –  Sep 01 '13 at 18:32
  • @H2CO3 weird in the sense that can't use directly strigize_impl :P not personal – PlainOldProgrammer Sep 01 '13 at 18:41
2

If you're compiling with GCC you can call it with gcc -E filename.c to see the expansion of macros.

EDIT:

You can also use the stringize preprocessor operator # that effectively puts double-quotes around the right-hand symbol.

#include <stdio.h>

#define T(...) #__VA_ARGS__
#define paste(f, b) _paste(f, b)
#define _paste(front, back) front##back

int main()
{
    int n = 5;
    printf("macro expands to: '%s'\n", T(paste(number, 2), paste(number, 2)));
    printf("macro expands to: '%s'\n", T(paste(n, 2)));
    return 0;
}

This code hopefully answers the question.

You need to expand the macro one more time to expand the paste in the stringize. In other words, for the paste macro inside the stringize macro to expand, the preprocessor has to pass over the file one more time. That's why you pass it through another macro defined later in the file.

I'm not 100% sure of ALL the rules of the preprocessor, but this seems to hold pretty well. For every macro you want to expand inside another macro, you need to do some magic to force the preprocessor to pass over the file again :) There exist different ways of achieving this to my knowledge, but this is one.

EDIT2:

Edited the code. I am getting this output, is this what you want?

morten@laptop:/tmp$ ./a.out 
macro expands to: 'paste(number, 2), paste(number, 2)'
macro expands to: 'paste(n, 2)'
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55