Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: macro produced warning "statement with no effect"

I have following code in C:

int do_something(void);

#ifdef SOMETHING
#define DO_SOMETHING() do_something()
#else
#define DO_SOMETHING() 0
#endif

This code produced warning "statement with no effect" when compiled without SOMETHING defined. I am trying to fix it, but there is one problem - code which uses this macro sometimes checks that "return value" and sometimes ignores it. Because of this I cannot use the easiest solution - casting to void in macro itself.

Is it possible to write macro which allows to compare "returned value" and does not produce this warning when it is ignored?

I use gcc to compile my code.

like image 894
Daniel Frużyński Avatar asked Jul 20 '26 17:07

Daniel Frużyński


1 Answers

Three possible solutions:

Define a do_nothing function, which will get optimized out by gcc:

int do_something(void);
int do_nothing(void) { return 0; } 

#ifdef SOMETHING
#define DO_SOMETHING() do_something()
#else
#define DO_SOMETHING() do_nothing()
#endif

Or, modify the do_something implementation to move the #ifdef check there

int do_something(void)
{
  #ifndef SOMETHING
  return 0;
  #endif

// Your implementation here
}

You can also ignore the warning using #pragma directives.

By the way, which version of gcc and with which flags are you compiling? gcc -Wall -pedantic with GCC 4.9 doesn't produce the warning.

like image 94
gjulianm Avatar answered Jul 23 '26 08:07

gjulianm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!