Search notes:

Preprocessor: Stringify macros and identifiers

The single hash (#) directive (also known as stringizing operator) creates a quoted string from the value of a preprocessor macro.
#include <stdio.h>

//   QuoteIdent turns an unquoted identifier
//   into a (quoted) string:
//      QuoteIdent(foo) -> "foo"
#define QuoteIdent(ident) #ident

//   QuoteMacro can be used to turn the value
//  (rather than the name/ident)of the macro
//   into a string
#define QuoteMacro(macro) QuoteIdent(macro)


#define FRUIT   strawberry
#define I_LIKE  FRUIT

int main() {
  printf("QuoteIdent(int)    = %s\n", QuoteIdent(int)   );
  printf("QuoteIdent(FRUIT)  = %s\n", QuoteIdent(FRUIT) );
  printf("QuoteMacro(FRUIT)  = %s\n", QuoteMacro(FRUIT) );
  printf("QuoteMacro(I_LIKE) = %s\n", QuoteMacro(I_LIKE));
  printf("QuoteMacro(int)    = %s\n", QuoteMacro(int)   );
}
Github repository about-preprocessor, path: /hash/stringify.c
The program prints:
QuoteIdent(int)    = int
QuoteIdent(FRUIT)  = FRUIT
QuoteMacro(FRUIT)  = strawberry
QuoteMacro(I_LIKE) = strawberry
QuoteMacro(int)    = int

See also

Compare with ## which concatenates two macro values.
Preprocessor: macros

Index