C no op statement outside functions - c

I have code containing many lines such as:
static_assert(sizeof(my_stuct)==42, "check struct size");
and I want to compile on a compiler which has no static_assert implemented. So I want these lines to become no-op. I've tried:
#define static_assert(COND, MSG)
it works fine but I get a warning with clang compiler:
warning: extra ';' outside of a function [-Wextra-semi]
Is there a simple no-op C statement to be used outside functions which terminates with a semicolon and which can be used repeatedly?

I want to compile on a compiler which has no static_assert implemented. So I want these lines to become no-op.
Why not combining Lundin's answer (checking whether the current compiler has it implemented or not) with an implementation of static_assert (which is not hard to do)?
Copying the implementation from PDCLib (which is CC0 licensed):
/* with dodgy implementations, you could also #ifndef static_assert */
#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
#define _PDCLIB_cc( x, y ) x ## y
#define _PDCLIB_concat( x, y ) _PDCLIB_cc( x, y )
#define static_assert( e, m ) enum { _PDCLIB_concat( _PDCLIB_assert_, __LINE__ ) = 1 / ( !!(e) ) }
#endif
For a given expression e and message m, this declares an anonymous enumeration with one member, the name of which is _PDCLIB_assert_ concatenated with the current source file line (__LINE__) (so you can have multiple static_assert() per source file). This member is set to 1 divided by 1 if the expression e is true, or divided by 0 if the expression is false, which leads to output like this for a failed assert:
./path/to/source.c:57:94: warning: division by zero [-Wdiv-by-zero]
#define static_assert( e, m ) enum { _PDCLIB_concat( _PDCLIB_assert_, __LINE__ ) = 1 / ( !!(e) ) }
^
./path/to/header.h:571:1: note: in expansion of macro 'static_assert'
static_assert( false, "test" );
^
./path/to/source.c:57:62: error: enumerator value for '_PDCLIB_assert_571' is not an integer constant
...
It's not pretty, but it's perfectly C89-compliant, tested, and servicable.
Feel free to rename from _PDCLIB_* to anything you prefer.

My solution so far:
#define static_assert(COND, MSG) extern int __static_assert_dummy__
works fine but seems ugly to me!

Since this is a C11 feature, the answer is quite trivial: simply check if the compiler supports C11.
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112
static_assert(sizeof(my_stuct)==42, "check struct size");
#endif
The opposite, to make this a no-op in the absence of standard C, and thereby remove the need for the above compiler switches all over the place, is this:
#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
#define static_assert(cond, str) struct dummy_t
#endif
Though remember that static_assert requires #include <assert.h>, unlike the C11 keyword _Static_assert.

Tested only very quickly, but how about:
#define static_assert(COND, MSG) extern int _no_static_assertion
or something? Repeating an extern should be fine.

Related

Error: operator "defined" requires an identifier

I have been searching for an hour about this seemingly obvious question, and read several other posts including this one with the same title, but I am still struggling to find a convincing answer. Please forgive my ignorance in advance, and consider this code:
#define MY_MACRO 1
#define IMPLEMENT(x) (defined(x) && (x))
#if IMPLEMENT(MY_MACRO)
#define TESTVAL 1
#else
#define TESTVAL 0
#endif
#include <stdio.h>
int main()
{
printf("Value is %d\n", TESTVAL);
return 0;
}
Which doesn't compile and the error messages are:
error: operator "defined" requires an identifier
error: missing '(' in expression
One of the answers in the linked thread says the argument of IMPLEMENT is not known at compile-time, so we cannot use it with '#if defined'. But I don't think this is the case here. Can anyone explain why this doesn't compile?
Bonus question: is there any trick to safely replace the line #if defined(x) && x?
Can anyone explain why this doesn't compile?
IMPLEMENT(MY_MACRO) is expanding MY_MACRO to 1 and then replacing it for defined(1). defined(1) is invalid. 1 is not an identifier.
is there any trick to replace the line #if defined(x) && x?
Yes, just use #if x. If it's not defined, then it's zero. Just #if x, no need for a defined.
#if MY_MACRO

Defining C macros in preprocessor if statements

Below I change the value of the function that I call depending on the value of INPUT:
#include <stdio.h>
#define INPUT second
#if INPUT == first
#define FUNCTOCALL(X) first(X)
#elif INPUT == second
#define FUNCTOCALL(X) second(X)
#endif
void first(int x) {
printf("first %d\n", x);
}
void second(int x) {
printf("second %d\n", x);
}
int main() {
FUNCTOCALL(3);
return 0;
}
However, the output is first 3, even if INPUT is equal to second, as above. In fact, the first branch is always entered, regardless of the value of INPUT. I'm completely stumped by this - could someone explain what stupid mistake I'm making?
The c preprocessor only works on integer constant expressions in its conditionals.
If you give it tokens it can't expand (such as first or second where first and second aren't macros)
it'll treat them as 0 and 0 == 0 was true last time I used math. That's why the first branch is always taken.
6.10.1p4:
... After all replacements due to macro expansion and the defined
unary operator have been performed, all remaining identifiers
(including those lexically identical to keywords) are replaced with
the pp-number 0, and then each preprocessing token is converted into a
token. ...
You have no macros first and second defined. Be aware that the pre-processor is not aware of C or C++ function names!* In comparisons and calculations (e. g. #if value or #if 2*X == Y), macros not defined (not defined at all or undefined again) or defined without value evaluate to 0. So, as first and second are not defined, INPUT is defined without value, and the comparison in both #if expressions evaluates to 0 == 0...
However, if you did define the two macros as needed, they would collide with the C function names and the pre-processor would replace these with the macro values as you just defined them, most likely resulting in invalid code (e. g. functions named 1 and 2)...
You might try this instead:
#define INPUT SECOND
#define FIRST 1
#define SECOND 2
#if INPUT == FIRST
#define FUNCTOCALL(X) first(X)
#elif INPUT == SECOND
#define FUNCTOCALL(X) second(X)
#else
# error INPUT not defined
#endif
Note the difference in case, making the macro and the function name differ.
* To be more precise, the pre-processor is not aware of any C or C++ tokens, so it does not know about types like int, double, structs or classes, ... – all it knows is what you make it explicitly aware of with #define, everything else is just text it operates on and, if encountering some known text nodes, replacing them with whatever you defined.

C macros using enum

I am trying to use #if macros by defining the type of operation to invoke the right code, So i made a very simple example similar to what I am trying to do:
#include <stdio.h>
enum{ADD,SUB,MUL};
#define operation ADD
int main()
{
int a = 4;
int b = 2;
int c;
#if (operation == ADD)
c = a+b;
#endif
#if (operation == SUB)
c = a-b;
#endif
#if (operation == MUL)
c = a*b;
#endif
printf("result = %i",c);
return 0;
}
But unfortunately that does not work I get the following result = 8... if I replace The operation with numbers it works fine .... But i want it to work as it is described above.
Any help
The preprocessor is a step that is (in a way) done before the actual compiler sees the code. Therefore it has no idea about enumerations or their values, as they are set during compilation which happens after preprocessing.
You simply can't use preprocessor conditional compilation using enumerations.
The preprocessor will always consider that as false:
#if IDENT == IDENT
It can only test for numeric values.
Simplify your code and feed it to the preprocessor:
enum {ADD,SUB,MUL};
#define operation ADD
int main()
{
(operation == ADD);
}
The result of the preprocessor output is:
enum {ADD,SUB,MUL};
int main()
{
(ADD == ADD);
}
As you see, the enumerate value hasn't been evaluated. In the #if statement, that expression is just seen as false.
So a workaround would be to replace your enumerate by a series of #define:
#define ADD 1
#define SUB 2
#define MUL 3
like this it works. Output of preprocessor output is now:
int main()
{
int a = 4;
int b = 2;
int c;
c = a+b;
# 28 "test.c"
printf("result = %i",c);
return 0;
}
the solution is:
either rely at 100% on the preprocessor (as the solution above suggests)
or rely at 100% on the compiler (use enums and real if statements)
As others have said, the preprocessor performs its transformations at a very early phase in compilation, before enum values are known. So you can't do this test in #if.
However, you can just use an ordinary if statement. Any decent compiler with optimization enabled will detect that you're comparing constants, perform the tests at compile time, and throw out the code that will never be executed. So you'll get the same result that you were trying to achieve with #if.
But i want it to work as it is described above.
You seem to mean that you want the preprocessor to recognize the enum constants as such, and to evaluate the == expressions in that light. I'm afraid you're out of luck.
The preprocessor knows nothing about enums. It operates on a mostly-raw stream of tokens and whitespace. When it evaluates a directive such as
#if (operation == SUB)
it first performs macro expansion to produce
#if (ADD == SUB)
. Then it must somehow convert the tokens ADD and SUB to numbers, but, again, it knows nothing about enums or the C significance of the preceding code. Its rule for interpreting such symbols as numbers is simple: it replaces each with 0. The result is that all three preprocessor conditionals in your code will always evaluate to true.
If you want the preprocessor to do this then you need to define the symbols to the preprocessor. Since you're not otherwise using the enum, you might as well just replace it altogether with
#define ADD 1
#define SUB 2
#define MUL 3
If you want the enum, too, then just use different symbols with the preprocessor than you use for the enum constants. You can use the same or different values, as you like, because never the twain shall meet.
Another solution would be to have the enum in an included header file.

C - Macro contradiction

I just started learning C and I am not sure about how everything works. Some of examples simply looks weird to me and I cannot predict what would it print. I am programming in GCC, I mean it is my compiler, so every code I test is in GCC.
I understood pointers etc how that all stuff work, but also as you know there are macros. Macros are very weird, and I discovered one example I have no explanation about. For example look at this code particular code:
#include <stdio.h>
int main(void){
#define A 1
if(A == 0){
#define B 1
}
#ifdef B
printf("B is defined, which means that A == 0.\n");
#elif
printf("B isn't defined, which means that A == 1.\n");
#endif
printf("Value of A is %d\n", A);
return 0;
}
What should it print? Well, I though it is easy to guess, but now I see that it is not very easy. The result is not what i thought is.
This was my reasoning: we firstly define A to be 1 and then if A is 0 then define B to be 1. So, if B is defined it means that A == 0 and otherwise A == 1. But, surprisingly it prints:
B is defined, which means that A == 0.
Value of A is 1
What? It just printed a contradiction. I am not sure if this is some weird thing in GCC (because I have no visual studio or other compiler to test it). However, I am 99% sure I somehow misunderstood macros. I tried using variables instead of macros and it works properly.
Is it an issue, or did I simply misunderstand how macros work? Please keep in mind that I'm C beginner, so take it easy :).
Thank you in advance.
Macros are part of the preprocessor before the code is actually compiled. The compiler will search the code for all preprocessor directives and modify the code accordingly. So when the compiler gets to if(A == 0) {#define B 1} it has already been expanded to if(0 == 1) { }.
Macros makes it easy to configure an aspect of your code or to replace magic numbers into meaningful defines. For example:
#define DO_HOUR_CALCULATION
#define NUMBER_OF_SECONDS_PER_MINUTE (60)
#define NUMBER_OF_SECONDS_PER_HOUR (NUMBER_OF_SECONDS_PER_MINUTE * 60)
...
#ifdef DO_HOUR_CALCULATION
int hours = time / NUMBER_OF_SECONDS_PER_HOUR;
int minutes = (time % NUMBER_OF_SECONDS_PER_HOUR) / NUMBER_OF_SECONDS_PER_MINUTE;
#endif // defined(DO_TIME_CALCULATION)
Another use is to simplify a repeated or configurable task:
#define DEBUG
#ifdef DEBUG
#define DPRINTF(fmt, args...) printf(fmt, ## args)
#else
#define DPRINTF(args...) do {} while (0)
#endif
...
ret = read(fd, p_buffer, buf_size);
DPRINTF("read(fd) returned %d\n", ret);
Hope this helps you and happy coding!
Preprocessing directives are processed before non-preprocessing tokens are translated. And particularly, preprocessing directives are always evaluated at compile time, and never at runtime (cf. C standard, e.g. this draft).
If a token is a preprocessing token is defined as follows (all others are non-preprocessing tokens):
6.10 Preprocessing directives
(2) A preprocessing directive consists of a sequence of preprocessing
tokens that begins with a # preprocessing token that (at the start of
translation phase 4) is either the first character in the source file
(optionally after white space containing no new-line characters) or
that follows white space containing at least one new-line character
So if(A == 0) are non-preprocessing tokens (as the respective line does not start with a #, optionally with some space characters before), while #define B 1 is a preprocessing token.
According to the translation phases, preprocessing tokens are evaluated at translation time before the other tokens:
5.1.1.2 Translation phases
(4) Preprocessing directives are executed, macro invocations are
expanded, and _Pragma unary operator expressions are executed.
(7) ... The resulting tokens are syntactically and semantically
analyzed and translated as a translation unit.
So B will be defined already before if(A==0) will be translated, and particularly before if(A==0) will be executed. Thus, B will always be defined in your program.
The #define B 1 does not occur at run time. It happens at compile time. So #define B 1 is part of the compilation regardless of the result of A == 0, a run-time comparison
if(A == 0){
#define B 1
}
The following code
#ifdef B
printf("B is defined, which means that A == 0.\n");
#elif
printf("B isn't defined, which means that A == 1.\n");
#endif
is then the same as
printf("B is defined, which means that A == 0.\n");

How can I get the gcc preprocessor to check if an expression evaluates to a value or nothing?

I'm using gcc (specifically avr-gcc).
So this is my conundrum:
Let's say I have these defined somewhere:
#define THING_0_A 0
#define THING_0_B 1
#define THING_1_A 0
Then in a second file I have this:
#define CONCAT_(A,B,C) A ## B ## C
#define CONCAT(A,B,C) CONCAT_(A,B,C)
#define ID 0
#define THING_N(A) CONCAT(THING_,ID,A)
With this I now have a selection of expressions (still in the second file):
THING_N(_A) // evaluates to 0
THING_N(_B) // evaluates to 1
THING_N(_C) // evaluates to... nothing? Or undefined? Or THING_0_C?
Now, what I'm trying to work out is how to do this (also still in the second file):
#ifdef THING_N(_A)
// Do something knowing that THING_N(_A) is defined (in this case THING_0_A)
#endif
Or:
#if THING_N(_A)
// Do something knowing that the value THING_N(_A) evaluates to is defined and not just "nothing"
#endif
Of course neither of these work because #ifdef can't take an expression as an argument (and it would end up as "#ifdef 0" anyway), and THING_N(_A) evaluates to 0 within #if.
In other words, I'm looking for a way to make the preprocessor evaluate:
THING_N(_A) to true
THING_N(_B) to true
THING_N(_C) to false
THING_N(_D) to false
etc...
to be used in a conditional.
Try this:
#if (1-THING_N(_A)-1 != 2)
This will be true for every value of THING_N(_A) (except for the value -2). It will be false only if THING_N(_A) is undefined or defined empty.
If there is a chance your macro can expand to -2, you can modify the second 1 and the 2 to other literals of your choice so that the basic idea holds.
You can concatenate the value with a known value, and check the result:
#define EMPTY_VAL_HELPER(VAL) VAL ## 1
#define EMPTY_VAL(VAL) EMPTY_VAL_HELPER(VAL)
Now you could do:
#if defined(FOO) && (EMPTY_VAL(FOO) == 1)
//FOO is defined, but empty
#endif
#if defined(FOO) && (EMPTY_VAL(FOO) != 1)
//FOO is defined, and not empty
#endif
So, after doing a bunch of research and experimenting I've concluded that there is no solution to my question, and the reason for this is actually quite simple.
Given the setup I have in my question this is what the expressions are evaluated to:
THING_N(_A) -> THING_0_A -> 0
THING_N(_B) -> THING_0_B -> 1
THING_N(_C) -> THING_0_C -> 0 (Not nothing, as I previously thought.)
The piece of information I was missing earlier was that, as per c99 standard, undefined identifiers will be converted to 0, not nothing, making it impossible to distinguish between something undefined and something defined as 0.
Had the c99 standard chosen to make undefined identifiers evaluate to nothing, just like #define FOO makes FOO evaluate to nothing, then atturri's solution would have worked.
Oh well.

Resources