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按字节设置

 

posted @ 2021-08-01 18:19  yiwenzhang  阅读(484)  评论(0编辑  收藏  举报