#if with no expression

Guest #1413782
Rate this post
useful
not useful
Hi,

I've got the following code fragment and getting the error as indicated. 
I have no clue why it doesn't work, it seems to be alright for me... 
Google didn't help either, just tons of kernel compilation bug reports 
etc. but no solution. Any help is appreciated!

1
#define XYZ
2
#define WITHB
3

4
#ifdef XYZ
5
 #ifdef WITHA
6
        if (count < 1000) {
7
            save[count] = (short)IP;
8
            count++;
9
        }
10
 #elif WITHB                           //<-- ERROR: #if with no expression
11
        if (count < 1000) {
12
            save[count] = temp;
13
            count++;
14
        }
15
 #endif
16
#endif
Guest #1413809
Rate this post
useful
not useful
1
 #elif WITHB                           //<-- ERROR: #if with no expression
This expression equals to
1
 #else if WITHB == TRUE                          //<-- ERROR: #if with no expression

but since you only defined WITHB (without a value) it is expanded to 0, 
which equals FALSE.

Try either to define WITHB with a value or change your if-statement as 
follows:
1
 #else ifdef WITHB

Regards,

Ralf
#1414276
Rate this post
useful
not useful
Rolf Magnus wrote:
> #elif  is not the same as a combination of #else and #ifdef, but rather
> #else and #if. And #if requires an expression, which is missing if the
> macro isn't defined.

Rolf makes a good point, but a better solution is to use the improved 
preprocessor syntax introduced in ANSI C in 1989! Despite its 20 year 
history, this K&R syntax is still prevalent; I have no idea why. This is 
what you need (already suggested by Stefan Ernst rather more succinctly 
and without the rant):
1
#define XYZ
2
#define WITHB
3

4
#if defined XYZ
5
 #if defined WITHA
6
        if (count < 1000) {
7
            save[count] = (short)IP;
8
            count++;
9
        }
10
 #elif defined WITHB
11
        if (count < 1000) {
12
            save[count] = temp;
13
            count++;
14
        }
15
 #endif
16
#endif

That way you never need to assign a value to the macros; their mere 
existence is enough.

I have no idea what "Guest" was talking about; in what language is 
#elsif valid I wonder!? Certainly non-standard, and not documented in 
the GNU C preprocessor: 
http://gcc.gnu.org/onlinedocs/cpp/Index-of-Directives.html#Index-of-Directives

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now