1. 程式人生 > 其它 >c語言學習6---巨集&&字串

c語言學習6---巨集&&字串

1.列印LOG巨集

#include <stdio.h>// 兩個井號連線字串,避免agrs為空
// 條件式編譯 gcc -D DEBUGGER
​
#ifdef DEBUG
#define log(frm, args...){\
    printf("[%s : %s : %d]",__FILE__,__func__, __LINE__);\
    printf(frm,##args);\
    printf("\n");\
}
#else 
#define log(frm,args...)
#endif
#define contact(a,b) a##bint main() {
    
int a = 123, b=234,abc, def; int abcdef = 0; printf("[%s : %d] a = %d\n",__func__,__LINE__,a); contact(abc, def) = 1; log("%d",b) return 0; }

2.優於主函式執行的巨集&&泛型巨集

#include <stdio.h>
// 優於主函式執行的巨集__attribute__((constructor))
__attribute__((constructor))
void func() {
    
int a = 2, b = 3; printf("func: %d + %d = %d", a, b, a + b); return; } // 泛型巨集 #define TYPE(a) _Generic((a),\ int : "%d",\ double : "%.2lf",\ char * : "%s"\ ) ​ int main() { int a = 123; double b = 3.24; char str[] = "hello world"; printf(TYPE(a),a); printf(TYPE(b), b); printf(TYPE(str), str);
return 0; }

3.字串

memset按位元組設定