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.
Related
This question already has answers here:
What is the value of an undefined constant used in #if?
(3 answers)
Closed 12 days ago.
May be this is duplicate, I can't find similar question.
My surprise that, following code works for all three big compiler without error
#include <cstdio>
int main() {
#if !_LIBCPP_VERSION
std::printf("_LIBCPP_VERSION not defined");
#else
std::printf("_LIBCPP_VERSION defined and equal to %d", _LIBCPP_VERSION);
#endif
#ifndef _LIBCPP_VERSION
std::printf("_LIBCPP_VERSION not defined");
#else
std::printf("_LIBCPP_VERSION defined and equal to %d", _LIBCPP_VERSION);
#endif
}
Link to godbolt
My question is that: There check #if !_LIBCPP_VERSION - is always similar with #ifndef _LIBCPP_VERSION by standard C or C++?
They are not equivalent for numerical values.
SOME_MACROS
#if !SOME_MACROS
#ifndef SOME_MACROS
x
True
False
1
False
False
0
True
False
undef
True
True
Now, you said you only care about numerical values, so only the middle rows are relevant here. Yet we see a difference in those two rows.
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?
This question already has answers here:
how to use #ifdef with an OR condition?
(2 answers)
Closed 2 years ago.
I want to use or in a preprocessor #ifdef directive. I've tried using the || operator but it hasn't worked. How can I do this?
#if defined(SOMETHIG1) || defined(SOMETHING2)
/* .... */
#endif
You can't.
#ifdef COND is short for #if defined(COND). It has no way to combine conditions.
But, you don't need to use #ifdef! If you write it out in full, you can make use of all the operators you need:
#if defined(COND1) || defined(COND2)
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
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.