↑点击上方蓝色字体,关注“嵌入式软件实战派”获得更多精品干货。
▍21. 行控制#line
也许你知道用__LINE__
可以输出行号,然而你试下这个:
#line 12345 "abcdefg.xxxxx"
printf("%s line: %d\n", __FILE__, __LINE__);
printf("%s line: %d\n", __FILE__, __LINE__);
不单止行号被改了,文件名也被改了,是不是我们可以用这个干点啥……想想?
▍22. C和C++代码混合编译
在C的头文件上面
#ifdef __cplusplusextern "C" {#endif
然后在头文件下面
#ifdef __cplusplus}#endif
▍23. 用查表法实现hex2str
直接上代码
void hex2str(const unsigned char* hex, int size, char* str){ char char_arr[17] = "0123456789ABCDEF"; for(int i = 0; i < size; i++) { str[3*i] = char_arr[hex[i]>>4]; str[3*i+1] = char_arr[hex[i]&0x0F]; str[3*i+2] = ' '; }}
▍24. 用sprintf实现hex2str
直接上代码
void hex2str(const unsigned char* hex, int size, char* str){ for(int i = 0; i < size; i++) { sprintf(&str[3*i], "%02X ", hex[i]); } }
▍25. 将变量名变字符串
如果想打印一个变量名和它的值,也许会这样:
unsigned int program_flag = 0xAABBCCDD;printf("program_flag: 0x%08X\n", program_flag);
对于你有很多这样的变量要打印,建议你做个宏函数:
#define PRINT_HEX_VAR(var) printf("%s: 0x%08X\n", #var, var);unsigned int program_flag = 0xAABBCCDD;PRINT_HEX_VAR(program_flag);
▍26. 获取结构体元素的偏移
#define offsetof(type, member) ( (size_t)&((type*)0->menber) )typedef struct{ char a; int b; }S; offsetof(S, b);
▍27. 根据结构体成员获取结构体变量指针
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)/** * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. * */#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
这个怎么玩?看看链表
struct list_head { struct list_head *next; struct list_head *prev;};struct ipstore{ unsigned long time; __u32 addr[4]; struct list_head list;};container_of(ist1->list, struct ipstore, list)
▍28. scanf
高级玩法
scanf(“%[^,]”, a); // This doesn’t scrap the commascanf(“%[^,],”,a); // This one scraps the commascanf(“%[^\n]\n”, a); // It will read until you meet '\n', then trashes the '\n'scanf(“%*s %s”, last_name); // last_name is a variable
这是啥意思,正则表达式先了解下?然后自己试试,理解会更深入。
▍29. 两个数相加可以不用+
号?
int Add(int x, int y){ if (y == 0) return x; else return Add( x ^ y, (x & y) << 1);}
▍30. 调试的时候打印数组
你是不是曾经为打印数组而烦恼,每次都要将元素一个个取出来?
#define ARR_SIZE(arr) (sizeof(arr)/sizeof(*arr))#define PRINT_DIGIT_ARR(arr) do{\ printf("%s: ", #arr); \ for(int i=0; i < ARR_SIZE(arr); i++) \ printf("%d ", arr[i]);\ printf("\n");\ }while(0) int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};PRINT_DIGIT_ARR(arr);
未完待续……
我打算写100条,你还不关注我?
关注公众号,输入“qjyq”可以获得所有已发布的“奇技淫巧”。
往期精彩内容推荐>>>
C语言的奇技淫巧之一
C语言的奇技淫巧之二
数组之间不能赋值?穿个马甲吧!
图文并茂,一文讲透C语言结构体内存对齐
图解栈(Stack)与队列(Queue)
基于C99规范,最全C语言预处理知识总结