Our code has versioning information hardcoded in printf in at least 20 different files like: printf("Software version v11.2");
This means changing 20 files everytime there is an update.
Instead i wish to use a macro and #include it in a common.h file, such that version update is just changing one macro, that's all.
I tried something like:
#include <stdio.h>
#define VERSION "v11.2"
int main()
{
printf("Trying to print macro: ", VERSION);
}
But this style of "string""string" works in Java not in C. Any ideas how to accomplish it?
We will use the gcc for compilation.
NOTE: The macro is also used in some typical *.rc files, where we can't use a variable, and somewhere these rc files are parsed using SQL query. So we can't use variables like char ver[]="v11.2"
Here are two possible solutions.
#include <stdio.h>
#define VERSION "v11.2"
int main()
{
// Let printf insert the string when doing the output.
printf("Trying to print macro: %s\n", VERSION);
// Let the compiler concatenate the strings.
puts("Trying to print macro: " VERSION);
// Let the compiler concatenate the strings, can be assigned to a variable.
const char buf[] = "Trying to print macro: " VERSION;
puts(buf);
}
printf("Trying to print macro: %s", VERSION);
Is is a string, %s
should work.
int main()
{
printf("Trying to print macro: %s", VERSION);
}
Try this
#define PRINT(format,args...)\
\
do { \
printf(" your data...");\
} \
} while(0)
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.