C语言的奇技淫巧之三

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 __cplusplus
extern "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 comma
scanf(%[^,],,a); // This one scraps the comma
scanf(%[^\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”可以获得所有已发布的“奇技淫巧”。

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值