#if with no expression

Guest #1413782
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 #1413808
#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.
Guest #1413809
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
Guest #1413812
> ... which is missing if the macro isn't defined.

Actually I meant "which is missing if the maco is defined, but empty". 
If it's not defined, that's ok.

So try:

#define WITHB 1
Guest #1413823
Wow, thats quick :-)

I tried #elsif before, but that resulted in some weired behaviour. 
Thanks to Ralf and Rolf's solution it works as intended now!

Thanks very much for your help!

1
#define WITHB 1
#1414276
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

A question of your own? This thread is over 17 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.