简体   繁体   中英

Macro within macro in C

Please help me solving this problem. This is sample code to get line number,file name and var args in C. When I tried to run this, I got some errors. I am sure there are many ways to do this. But I have to use existing code to achieve some functionality. Any help or suggestions would be much appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define MY_MACRO1(fmt, args...)                \
 {                                             \
   char buf_d[1024];                       \
   MY_MACRO2(__FILE__, __LINE__,call_func(buf_d,sizeof(buf_d),fmt,##args));   \
 }   

#define MY_MACRO2(__FILE__, __LINE__,fmt, ...)  printf("%s : %d -> %s : \n",__FILE__, __LINE__, __VA_ARGS__);

char * call_func(char *buf_t, size_t size, const char *fmt, ...)
{
  va_list ap;
  va_start(ap,fmt);
  vsnprintf(buf_t, size, fmt, ap);
  va_end(ap);
  return buf_t;
}


int main()
{
  printf("\n Now I am printintg macro....\n");
  MY_MACRO1("Macro is working fine..\n");
  return 0;
}

Output:

Please find macro expansion. Last argument in macro (func return value) is missing.

char buf_d[1024];
printf("%s : %d -> %s : \n","file.c",35, );;

Error:

file.c:35:83: error: expected expression
{ char buf_d[1024]; printf("%s : %d -> %s : \n","file.c", 35, );; };
                                                              ^

1 error generated.

What a mess! Let's clean the things:

The code:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


#define DEBUG_MSG( _msg, ... )  do {print_debug_msg( __FILE__, __LINE__, __FUNCTION__, _msg, ## __VA_ARGS__ ); }while(0)


void print_debug_msg( const char * module, int line, const char * func, const char * fmt, ... )
{
    va_list va;
    char buf[ 1024 ] = {0};

    va_start( va, fmt );
    vsnprintf( buf, sizeof(buf), fmt, va );
    va_end( va );

    printf( "%s:%d - %s() - %s\n", module, line, func, buf );
}


int myfunc( const char * msg )
{
    DEBUG_MSG( "Message: %s",  msg );

    return 0;
}


int main( int argc, char * argv[] )
{
    DEBUG_MSG("The quick brown fox jumps over the lazy dog.");

    DEBUG_MSG("Pack my box with five dozen liquor jugs.");

    myfunc( "How vexingly quick daft zebras jump" );

    myfunc("The five boxing wizards jump quickly.");

    return 0;
}

/* eof */

Compiling:

$ gcc -Wall macro.c -o macro

Testing:

$ ./macro 
macro.c:32 - main() - The quick brown fox jumps over the lazy dog.
macro.c:34 - main() - Pack my box with five dozen liquor jugs.
macro.c:24 - myfunc() - Message: How vexingly quick daft zebras jump
macro.c:24 - myfunc() - Message: The five boxing wizards jump quickly.

References:

1) Recommended C Style and Coding Standards (Macros)

2) GCC Manual - Standard Predefined Macros

Hope it Helps!

When you use , ... that means that you MUST supply an argument, which is not your case. You can just modify your macro by naming the argument list and let the preprocessor elude the comma if needed:

#define MY_MACRO1(fmt, args...)                \
 {                                             \
   char buf_d[1024];                       \
   MY_MACRO2(__FILE__, __LINE__,call_func(buf_d,sizeof(buf_d),fmt,##args)); \
 }   

#define MY_MACRO2(F,L,fmt,args...)  printf("%s : %d -> %s : \n",F, L, fmt, ##args)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM