What does this code mean? "#define TO_LITERAL_(text) #text"

324 views Asked by At

When I searched the meaning of __cplusplus, I found a piece of code as following.

#include <stdio.h>

int main() {
#define TO_LITERAL(text) TO_LITERAL_(text)
#define TO_LITERAL_(text) #text
#ifndef __cplusplus
/* this translation unit is being treated as a C one */
    printf("a C program\n");
#else
// this translation unit is being treated as a C++ one
    printf("a C++ program\n__cplusplus expands to \""
          TO_LITERAL(__cplusplus) "\"\n");
#endif
    (void)getchar();
    return 0;
}

This code gives different output according to which way it's compiled. But I don't know well about the two bold lines.

  1. Why it's wrong if I combine these two lines into one line: #define TO_LITERAL(text) #text
  2. What's the meaning of #text in the second line?

Thank you so much

0

There are 0 answers