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.
You can define a macro with #define directive. In C, A macro is any constant value or variable with its value. And the macro name will get replaced by its value in the entire program. Macros help in writing less code and also in saving time.
In C programming language, the macro is not less than similar to functions, but macro has built-in macro also.
This macro can be a pointer variable or identifier or variable defined using #define and it is replaced by its value. This is mainly used when numeric constants are required. In this example, we can use “MAX” as a macro name with value 100 which will be replaced with “MAX”.
Macros in C are very useful at multiple places to replace the piece of code with a single value of the macro. Macros have multiple types and there are some predefined macros as well.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With