C语言中一些非常酷的技巧(cool tricks)
来自Quora,认为不错,就实践了一下。
1. #if 0 ...... #endif 块中的内容不会被编译,由于凝视不同意嵌套,我们能够把临时不用的代码块放在
这里面。
2. 数组初始化的时候能够指定索引,并且能够给特定范围的数组赋值。
比方 int array[] = { [0 ... 9] = 1, [10 ... 20] = 2, [30 ... 40] = 3};
等价于 int array[] = { 1, 1, 1, 1, ..... 2, 2, 2 ...... 3, 3, 3};
演示样例:
#if 0 here will not enter into compile we can move the code we not need now here #endif #include<stdio.h> int main(){ int i = 0; int arr[] = {[1]=5, [5]=10, [2]=20}; int arr2[] = {[0 ... 9] = 10, [10 ... 19] = 20, [20 ... 29] = 30}; for(i = 0; i < 6; i++) printf("arr[%d] = %d ,", i, arr[i]); printf("\n"); for(i = 0; i < 30; i++) printf("arr2[%d] = %d ,", i, arr2[i]); return 0; }
3. 要巧妙使用scanf,支持正则式,并且能够超越每次读到空白就终止的限制。
演示样例:
#include<stdio.h> #define MAX 100 int main(){ int i = 0; char s1[MAX],s2[MAX]; //scanf("%[^\n]\n",s1); //read till meet '\n',and then trash the \n //scanf("%[^,]",s1); //?? also will trash the coma //scanf("%[^,],",s1); // this does not trash the coma //this * can make us skip some input //for example,we just care the last-name scanf("%*s %s",s1); printf("%s\n",s1); return 0; }
4. 理解offset宏定义,求一个成员在结构体中的偏移量。
演示样例:
#include <stdio.h> #include <stdlib.h> #define offset(TYPE, MEMBER) ((size_t)(&((TYPE *)0)->MEMBER)) int main(){ struct test{ int a; int b[4]; int c; }; printf("offset a : %lu\n", offset(struct test, a)); printf("offset b : %lu\n", offset(struct test, b)); printf("offset c : %lu\n", offset(struct test, c)); struct test *t = (struct test *)0; printf("%d\n", &(t->c)); // right printf("%d\n", t->c); //?but cannot do this, core dump return 0; }
5. s[i] 是 *(s + i) 的语法糖,所以等价于 i[s]。
演示样例:
#include <stdio.h> int main(){ char s[] = "vonzhou"; printf("%c\n", 2[s]); return 0; }
6. 再次是printf 和 scanf 的技巧。
#include <stdio.h> int main(){ int n = 6; int val = 1000; char s[100]; printf("%*d", n, val);// with a minimum width of n,default right aligned printf("hello\n"); //scanf("%*d");//read an integer and ignore it //scanf has regex built into it //read only selected chars into a string //scanf("%[abcd]s", s); //read everything excepted heading with abcd //scanf("%[^abcd]s" ,s); /* some complain that scanf reads upto a whitespace, so this trick can be used to consume everything upto a newline */ scanf("%[^\n]s", s); // reads all chars (including whitespaces) till newline is encountered. printf("s = %s\n", s); /* Another trick with scanf is to consume what is required. For example - If a user enters his date of birth in YYYY-MM-DD then you can directly read the year month and date into integer variables */ //scanf("%d-%d-%d", &yy, &mm, &dd); /* where yy, mm, dd are variables. Note that this will work only with that format of input. Anything else like YYYY/MM/DD,and the program will mostly crash. You specify a hyphen, it expects only a hyphen! */ return 0; }
7.利用scanf的返回值来识别是否到达文件尾EOF;当errno=0时%m格式会输出“Success”;brk(0)能够作为 return 0 的替代;
演示样例:
#include <stdio.h> int main(){ int n; //util reach EOF(ctrl + D) while(~scanf("%d",&n)){ //logic process printf("%d^2 = %d\n", n, n*n); } // %m print success only if errno = 0 printf("%m\n"); brk(0); }
7.变量会被隐式初始化为0和1;
演示样例:
<pre name="code" class="cpp">#include <stdio.h> // ?? // implicit initiliazation of variables with 0 and 1 g;main(i){ printf("%d,%d\n", g, i); return 0; }