I want to write a variadic macro that somehow knows the names of the arguments passed. For example:
The code:
int x = 2;
float f = 4.6;
char c = 'A';
char* str = "Bla bla";
PRINT("%d %f %c %s", x, f, c, str); // calling the macro
shall produce the output
x=2 f=4.6 c=A str=Bla bla.
Hope someone knows the answer to that.
Close but not exactly (only works for single expression) what the asker required:
#define PRINT(fmt, var) printf(#var " = " fmt, (var))
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#define PRINT(fmt, var) printf(#var " = " fmt, (var))
int
main(int argc, char *argv[])
{
int x = 2, y = 3;
float f = 4.6;
char c = 'A';
char *str = "Bla bla";
PRINT("%d\n", x);
PRINT("%f\n", f);
PRINT("%c\n", c);
PRINT("%s\n", str);
PRINT("%d\n", x+y);
exit(EXIT_SUCCESS);
}
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