https://jorenar.com/blog/less-known-c
阅读并摘录了一些有趣的内容,如下:
1 2 3 4 5 6 7 8 9 10
| int x = 42;
int func() { int x = 3840; { extern int x; return x; } }
|
0 位字段
1 2 3 4 5
| struct bar { unsigned char x : 5; unsigned short : 0; unsigned char y : 7; }
|
1 2 3 4
| char pad pad short boundary | | | | v v v v xxxxx000 00000000 yyyyyyy0
|
Duff’s device
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| send(to, from, count) register short *to, *from; register count; { register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }
|
idx[arr]
1 2 3 4 5 6 7
|
boxes[products[myorder.product].box].weight;
myorder.product[products].box[boxes].weight;
|
负数组索引
1 2 3 4
| int *end = arr + (len - 1); if (end[0] == VAL && end[-1] == VAL && end[-5] == VAL) { puts("Correct padding"); }
|
使用&&和||作为条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <ctype.h> #include <stdio.h> #include <stdbool.h>
int main(void) { 1 && puts("Hello"); 0 && puts("I won't"); 1 && puts("World!"); 0 && puts("be printed"); 1 || puts("I won't be printed either"); 0 || puts("But I will!");
true && (9 > 2) && puts("9 is bigger than 2");
isdigit('9') && puts("9 is a digit"); isdigit('n') && puts("n is a digit") || puts("n is NOT a digit!");
isalpha('a') && !puts("I'll be printed") || puts("But me AS WELL!");
return 0; }
|