I have this code:
#define PRINT(fmt, ...) printf("%s:%d: "fmt, __FILE__, __LINE__, ## __VA_ARGS__)
It works well when I pass some value to it, eg PRINT("Hello, world").
But when I pass variable as an argument, eg. PRINT(somevar) it doesn't print the value of variable int somevar. How should I rewrite the macros to make it print variables as well?
fmt(first) argument to the macro can be concatenated with the"%s:%d: "string, which means it too must be a literal. To printint somevar, the macro invocation must bePRINT("%d\n", somevar);.printfasprintf(some_int_var)either; a format string is needed. This is no different (and in fact mandatory as a const-literal, as your logic now mandates it as concat-able, and a computed format string is not feasible, fair warning). The only immediate you can get away with sending to this is just likeprintf, achar*(const or otherwise) itself, and just likeprintfthat would be discouraged for security reasons.