gcc - C macro with flexible argument -


i need :

#include <stdio.h> #include <stdint.h> #include <stdarg.h>  #define incr(count,args...) \    if(args) \    count++; \  void main() {     int count =1;     int flag =1;     incr(count);     printf("count %d",count);     incr(count,flag); /* flag determines if count incremented or not */     printf("count %d",count); } 

i following errors:

     sh-4.3$ gcc -o main*.c                                                                                                                                                   main.c: in function 'main':                                                                                                                                               main.c:6:8: error: expected expression before ')' token                                                                                                                    if(args) \                                                                                                                                                                       ^                                                                                                                                                                 main.c:15:5: note: in expansion of macro 'incr'                                                                                                                                incr(count);                                                                                                                                                              ^                                                                                                                                                                    sh-4.3$ 

here counter supposed incremented if flag present. need macro flexible number of arguments. please me on this

based on example in https://stackoverflow.com/a/11763277/5085250 can this:

#define get_macro(_1,_2,name,...) name #define incr(...) get_macro(__va_args__, incr2, incr1)(__va_args__)  #define incr1(count)\ count++;  #define incr2(count,flag)\ if(flag)count++; 

here assume want increment if no flag given. if don't want increment in case need modify incr1 part...


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -