MOHAMED MOHAMED - 1 year ago 126
C Question

How to define macro function which support no input parameter and support also input parametr in the same time

I want to define a macro function which support at the same time:

1) No input parameter

2) Input parameters

some thing like that:

#define MACRO_TEST(X)\
printf("this is a test\n");\
printf("%d\n",x) // the last printf should not executed if there is no input parameter when calling the macro

In the main:

int main()
MACRO_TEST(); // This should display only the first printf in the macro
MACRO_TEST(5); // This should display both printf in the macro

Answer Source

You can use sizeof for this purpose.

Consider something like this:

#define MACRO_TEST(X) { \
  int args[] = {X}; \
  printf("this is a test\n");\
  if(sizeof(args) > 0) \
    printf("%d\n",*args); \
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download