Can a MACRO be compared in #if with non numeric value [duplicate] - c++

This question already has answers here:
How to compare strings in C conditional preprocessor-directives
(14 answers)
Closed 6 years ago.
consider the following
#if TABLE_SIZE>200
#undef TABLE_SIZE
#define TABLE_SIZE 200
The macro TABLE_SIZE is being compared
is it possible that its compared with a non numeric value like,
#if MACRO==ABCDEF123
I tried it but compiler complains of ABCDEF123 not being defined and assumes it as 0.

For true portability, the expression in #IF expression can only contain integer and character constants. The C and C++ preprocessors can also evaluate +, -, *, /, <<, >>, !=, == and the two logical operators && and || which obey the short-circuiting rules of standard C and C++.
So no, you can't compare a string directly.

Related

How to get C compiler #error if a sizeof(struct ...) not equal to a given number? [duplicate]

This question already has answers here:
How can I use "sizeof" in a preprocessor macro?
(13 answers)
Closed 3 years ago.
How to get C compile time #error if a sizeof(struct ...) not equal to a given number?
The question is from programming course, where I'd like to avoid to run miss-sized binary code.
(The sizeof operator, as we know, doesn't work in #if .. #endif directive.)
How to get C compile time #error if a sizeof(struct ...) not equal to a given number?
You cannot, because the pre-processor knows nothing about sizes of types.
You can however static_assert:
static_assert(sizeof(T) == N, "T must have size N")
In C, the keyword is _Static_assert, also available through macro static_assert in <assert.h>.
Don't. You already explained why.
In modern C++ you can write:
static_assert(sizeof(T) == 42);
Although it is better to write code that doesn't care what the size of T is.
#include <assert.h>
//T should have size 10
static_assert(sizeof(T) == 10)
It's available only the latest C compiler

Double boolean negation operator [duplicate]

This question already has answers here:
Double Negation in C++
(14 answers)
double negation in C : is it guaranteed to return 0/1?
(2 answers)
Closed 5 years ago.
I came across this piece of code from the Microsoft implementation of GSL (the C++ Guideline Support Library):
#if defined(__clang__) || defined(__GNUC__)
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define GSL_LIKELY(x) (!!(x))
#define GSL_UNLIKELY(x) (!!(x))
#endif
I read about the __builtin_expect (here and here), but it is still unclear to me what is the purpose of the double boolean negation operator in (!!(x)). Why is it used?
The file in question is this one.
__builtin_expect works with strict equality, so the point of double negation here is to make sure all truthy values are converted to 1 (and thus match the 1 in GSL_LIKELY), and all the falsy values match the 0 in GSL_UNLIKELY.
The double negation is kept even if __builtin_expect is not available to keep uniformity (as the caller may store the return value for other uses besides as a condition in an if).

How to use logical XOR operator in macro? NOT how to defined XOR operator by macro [duplicate]

This question already has answers here:
Why is there no ^^ operator in C/C++?
(7 answers)
Logical XOR operator in C++?
(11 answers)
Closed 6 years ago.
Is there any tricky way to use logical xor operator ^^ in macro in C, C++, Objective-C?
I have tried applying ^^ directly in Objective-C, it does not work.
Edited: let me clarify my answer.
What I want is to use xor operator in macro. It does not mean "how to define the xor operator by a macro.
I.e, I want something like
#if defined(x) ^^ TARGET_OS_IOS ^^ __cplusplus
For seconds after posting the question, I figured out the answer my self.
!(A) != !(B) will be equivalent to xor operator
A better solution in case the number of operands is different than 2
!(A) ^ !(B) ^ !(C) ^ ...

Purpose of and() function in C++ [duplicate]

This question already has answers here:
or is not valid C++ : why does this code compile?
(3 answers)
Closed 9 years ago.
What is the function of and() in C++ and its syntax?
P.S. I happened to write out and() as a function and the C++ text editor highlighted it.
Even after much searching I could not find its function or the syntax.
There is no and function in C++, it's a reserved identifier, the same as the logical operator &&.
C++11(ISO/IEC 14882:2011) §2.5 Alternative tokens
In C, there's no and keyword, but if you include the header iso646.h, and is the same as && as well.
C11(ISO/IEC 9899:201x) §7.9 Alternative spellings
The header <iso646.h> defines the following eleven macros (on the left) that expand
to the corresponding tokens (on the right):
and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=
and is not a function; it is an operator. It means the same thing as &&. For example,
x && y
and
x and y
mean the same thing.
If you try to use it as a function, it will give you an error.
See this answer for more information on and, or, etc.

What is the usage of "!!" (negating twice)? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Double Negation in C++ code
Let's say:
bool var = !!true;
It will assign "true" to the variable. Seems useless, but I was looking at Visual Studio's definition of "assert", and it is:
#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
Why does it negate the "_Expression" twice?
I wonder that they want to force the "!" operator to be called (in the case it is overloaded), but that doesn't seem to be a good reason.
!! guarantees that the result will end up as a 1 or a 0, rather than just the value of _Expression or 0. In C, it's unlikely to matter, but in C++ I think it turns the result of the expression into a bool type, which might be useful in some cases. If you did have some API that required a literal 1 or 0 be passed to it, using !! would be a way to make it happen.
It's possible that you might want an int variable that's either 1 or 0.
So you can't for example pass a 5, instead the double negation would turn that 5 into a 1.
Also, have a look at how TRUE is defined:
#ifndef TRUE
#define TRUE 1
#endif
Therefore, an expression like:
int x = 5;
if ( x == TRUE )
{
//....
}
would not pass, whereas
if ( x )
{
//....
}
would.
Its use is to make sure the value is either 0 or 1. I think it's superfluous with C++'s bool type.