Home » Errors & Failures » C program Errors » Fixed : error: #elif with no expression

Fixed : error: #elif with no expression

When we want to make a compile time decisions to enable some code and disable other, #ifdef and #elif helps us to achieve this, but sometimes we get the error as mentioned in this post.

We written a demo code as below,

#include <stdio.h>

#define ENABLE_SUBTRACTION_CODE

int main(void) {
        int a = 10, b = 5;
#ifdef ENABLE_SUM_CODE
        return a + b;
#elif ENABLE_SUBTRACTION_CODE
        return a - b;
#else
        printf("No active code");
#endif
        return 0;
}

when we compiled the above C code, which tries to enable subtraction code by defining macro ENABLE_SUBTRACTION_CODE at the top, we got following error,

helloworld.c: In function ‘main’:
helloworld.c:10:31: error: #elif with no expression
   10 | #elif ENABLE_SUBTRACTION_CODE
      |                               ^

Solution :

This error is because #elif also expected the expression check as we have done with #ifdef.

hence, when we change below line of code

#elif ENABLE_SUBTRACTION_CODE

to

#elif defined(ENABLE_SUBTRACTION_CODE)

So, the final working code looks as,

#include <stdio.h>

#define ENABLE_SUBSTRACTION_CODE

int main(void) {
        int a = 10, b = 5;
#ifdef ENABLE_SUM_CODE
        return a + b;
#elif defined(ENABLE_SUBSTRACTION_CODE)
        return a - b;
#else
        printf("No active code");
#endif
        return 0;
}

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment