0

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

Vadym Pechenoha
  • 574
  • 5
  • 17
LittleSoup
  • 31
  • 1
  • 3
  • 1
    To see "in action" what the answer describes (correctly), you may call the compiler with the option that performs the pre-processor only. For `g++`, it is `-E`. For MSVC, it is `/P`. – Scheff's Cat Jul 27 '17 at 12:44
  • 1
    About the meaning of `__cplusplus`, [read this](http://en.cppreference.com/w/cpp/preprocessor/replace). This code snippet prints which standard revision of C++ is in effect. C also has one, it's `__STDC_VERSION__` – StoryTeller - Unslander Monica Jul 27 '17 at 12:49

0 Answers0