How to fix `left hand operand has no effect` warning with variadic macro in C -


i'm using variadic macro simulate default argument. compile -wunused-value. thus, following warning:

warning: left-hand operand of comma expression has no effect 

is there way somehow fix warning without having remove -wunused-value? or have end using #pragma gcc diagnostic ignored "-wunused-value"?

#include <stdio.h>  #define sum(a,...) sum( a, (5, ##__va_args__) )  int sum (int a, int b) {   return + b; }  int main() {   printf("%d\n", sum( 3, 7 ) );   printf("%d\n", sum( 3 ) ); } 

the ## construct using gcc speciality , not portable. don't use it, there other ways.

the following should expect

#define sum2(a, b, ...) sum((a), (b)) #define sum1(...) sum2(__va_args__) #define sum(...) sum1(__va_args__, 5, 0) 

such games macro default arguments frowned upon many, because may make code more difficult read. moment i'd suggest don't use such constructs in programs. should perhaps learn more of basics before go such esoteric stuff.

also idea want silence compiler bad one. compiler there you, listen him. in contrary, raise warning level maximum , improve code until compiles without warning.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -