C/Macro Preprocessor/Macro String
Join string
<source lang="cpp">
- include <stdio.h>
- define JOIN(a, b) a ## b
int main(void) {
printf(JOIN("one ", "two")); return 0;
}
</source>
Output string using Macro
<source lang="cpp">
- include <stdio.h>
- define output(i) printf("%d %d\n", i ## 1, i ## 2)
int main(void) {
int count1, count2; int i1, i2; count1 = 10; count2 = 20; i1 = 99; i2 = -10; output(count); output(i); return 0;
}
</source>
Use macro to build string
<source lang="cpp">
- include <stdio.h>
- define MKSTRING(str) # str
int main(void) {
int value; value = 10; printf("%s is %d", MKSTRING(value), value); return 0;
}
</source>