-1

I need to do something like :

#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 the count is to be incremented or not */
    printf("count %d",count);
}

I get 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 the counter is supposed to be incremented only if the flag is present. I need a macro with flexible number of arguments. Please help me on this

Mat
  • 202,337
  • 40
  • 393
  • 406
Coder
  • 9
  • 3

1 Answers1

0

Based on the example in https://stackoverflow.com/a/11763277/5085250 you can do something like 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 I assume you want to increment if no flag is given. If you don't want to increment in that case you need to modify the INCR1 part...

Community
  • 1
  • 1
havogt
  • 2,572
  • 1
  • 27
  • 37
  • Great however, I get the following compile time error for the same:error: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro [-Werror] – Coder Jul 13 '15 at 05:13
  • In https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html there is a comment on compatibility about that problem: "If you are concerned with portability to previous versions of GCC, you should use only named variable arguments. On the other hand, if you are concerned with portability to other conforming implementations of C99, you should use only __VA_ARGS__." You can try replacing __VA_ARGS__ with a named argument like in your example. – havogt Jul 13 '15 at 06:53
  • Or you may try adding `-std=c99` to the compile options if that is ok for your setup. – havogt Jul 13 '15 at 06:57