这段代码是什么意思? “#define TO_LITERAL_(text)#text”

时间:2017-07-27 12:39:17

标签: c++ predefined-macro

当我搜索__cplusplus的含义时,我发现了一段代码如下。

#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;
}

此代码根据编译方式提供不同的输出。但我不太了解两条粗线。

  1. 如果我将这两行合并为一行,为什么会出错:#define TO_LITERAL(text) #text
  2. 第二行#text的含义是什么?
  3. 非常感谢

0 个答案:

没有答案