#line
也许你知道用__LINE__
可以输出行号,然而你试下这个:
#line 12345 "abcdefg.xxxxx"
printf("%s line: %d\n", __FILE__, __LINE__);
printf("%s line: %d\n", __FILE__, __LINE__);
不单止行号被改了,文件名也被改了,是不是我们可以用这个干点啥……想想?
在C的头文件上面
#ifdef __cplusplus
extern "C" {
#endif
然后再头文件下面
#ifdef __cplusplus
}
#endif
直接上代码
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] = ' ';
}
}
直接上代码
void hex2str(const unsigned char* hex, int size, char* str)
{
for(int i = 0; i < size; i++)
{
sprintf(&str[3*i], "%02X ", hex[i]);
}
}
如果想打印一个变量名和它的值,也许会这样:
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);
#define offsetof(type, member) ( (size_t)&((type*)0->menber) )
typedef struct
{
char a;
int b;
}S;
offsetof(S, b);
#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)
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
这是啥意思,正则表达式先了解下?然后自己试试,理解会更深入。
+
号?int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
你是不是曾经为打印数组而烦恼,每次都要将元素一个个取出来?
#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”可以获得所有已发布的“奇技淫巧”。