What does this code mean? "#define TO_LITERAL_(text) #text" [duplicate] - c++

This question already has answers here:
What concept is being exhibited in this macro wrapping?
(1 answer)
Stringification - how does it work?
(2 answers)
Stringify first level macro expansion C
(2 answers)
Macro expansion and stringification: How to get the macro name (not its value) stringified using another macro?
(1 answer)
What is the compiler seeing with this macro? [closed]
(4 answers)
Closed 5 years ago.
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.
Why it's wrong if I combine these two lines into one line: #define TO_LITERAL(text) #text
What's the meaning of #text in the second line?
Thank you so much

Related

_ADDRESSOF(v) macro definition in VS2019 C++ [duplicate]

This question already has answers here:
Implementation of addressof
(4 answers)
Why and when is cast to char volatile& needed?
(2 answers)
Closed last year.
In <vadefs.h> you'll find the following definition for the macro _ADDRESSOF(v) in VS2019:
#ifdef __cplusplus
#define _ADDRESSOF(v) (&const_cast<char&>(reinterpret_cast<const volatile char&>(v)))
#else
#define _ADDRESSOF(v) (&(v))
#endif
I'd like to understand the first definition above, when __cplusplus is defined. Why does it work?

What is the difference between #ifdef VALUE vs #if defined (VALUE) [duplicate]

This question already has answers here:
Is #if defined MACRO equivalent to #ifdef MACRO?
(5 answers)
Closed 4 years ago.
#include<stdio.h>
#define MAX 0
int main()
{
#ifdef MAX
printf("MAX defined");
#endif
#if defined (MAX)
printf("MAX is defined");
#endif
return 0;
}
Both the #ifdef and #if defined give the same effect then what is the difference between them? I have not seen the disassembly code of these directives if you have seen then kindly try to explain that as well.
The difference is historical. Originally there was only #ifdef. The newer syntax is more flexible and allows combining tests with logical conditions, but in the simple form you can use them interchangeably.

Using a macro expansion as part of an identifier [duplicate]

This question already has answers here:
Creating C macro with ## and __LINE__ (token concatenation with positioning macro)
(3 answers)
Why do I need double layer of indirection for macros?
(5 answers)
Closed 5 years ago.
Consider the following code:
struct S {};
#define CREATE_INSTANCE S instance_##__LINE__
int main()
{
CREATE_INSTANCE;
CREATE_INSTANCE;
return 0;
}
What I want it to do is create two instances of S named instance_7 and instance_8. What it actually does is creates instance___LINE__ twice.
How to achieve what I want?
Using some indirection:
#define Concat_(a, b) a ## b
#define Concat(a, b) Concat_(a, b)
#define CREATE_INSTANCE S Concat(instance_, __LINE__)

Pound sign in #define function parameters [duplicate]

This question already has answers here:
What does #x inside a C macro mean?
(4 answers)
Closed 4 years ago.
What does the pound sign indicate in this line of code?
#define CONDITION(x) if(!(x)){ HandleError(#x,__FUNCTION__,__LINE__);return false;}
This is how it is being called:
CONDITION(foo != false);
A single # before a macro parameter converts it to a string literal.
#define STRINGIFY(x) #x
STRINGIFY(hello) // expands to "hello"
In your example, the string would be "foo != false", so that the error message shows the code that was being tested.
A double ## between two tokens within a macro combines them into a single token
#define GLOM(x,y) x ## y
GLOM(hello, World) // expands to helloWorld

C- Preprocessor defining a macro [duplicate]

This question already has answers here:
Preprocessor #if directive
(3 answers)
Closed 8 years ago.
Hello I need a help in defining a macro dependent on a variable.
For Eg:
#if TEMP
#define COUNT 5
#else
#define COUNT 6
#endif
TEMP will be set in a function.
Eg:
void func()
{
TEMP = 1;
}
Setting the count macro and defining temp are in different files.
Can anyone help me on this?
TIA
Sowmya
This is impossible. TEMP will only be set on runtime and is not available for the preprocessing. You could use the -DTEMP as a compiler flag to define it if you need COUNT to be 5. You'd have to change your code into the following:
#ifdef TEMP
#define COUNT 5
#else
#define COUNT 6
#endif
This is not possible. Macros are resolved during compilation, so a runtime change like setting a variable cannot impact them.