来自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};

演示样例:

  1. #if 0
  2.  
  3. here will not enter into compile
  4. we can move the code we not need now here
  5.  
  6. #endif
  7.  
  8. #include<stdio.h>
  9.  
  10. int main(){
  11. int i = 0;
  12. int arr[] = {[1]=5, [5]=10, [2]=20};
  13. int arr2[] = {[0 ... 9] = 10, [10 ... 19] = 20, [20 ... 29] = 30};
  14. for(i = 0; i < 6; i++)
  15. printf("arr[%d] = %d ,", i, arr[i]);
  16. printf("\n");
  17. for(i = 0; i < 30; i++)
  18. printf("arr2[%d] = %d ,", i, arr2[i]);
  19. return 0;
  20. }

3. 要巧妙使用scanf,支持正则式,并且能够超越每次读到空白就终止的限制。

演示样例:

  1. #include<stdio.h>
  2. #define MAX 100
  3.  
  4. int main(){
  5. int i = 0;
  6. char s1[MAX],s2[MAX];
  7.  
  8. //scanf("%[^\n]\n",s1);
  9. //read till meet '\n',and then trash the \n
  10.  
  11. //scanf("%[^,]",s1); //?? also will trash the coma
  12. //scanf("%[^,],",s1); // this does not trash the coma
  13.  
  14. //this * can make us skip some input
  15. //for example,we just care the last-name
  16. scanf("%*s %s",s1);
  17.  
  18. printf("%s\n",s1);
  19.  
  20. return 0;
  21. }

4. 理解offset宏定义,求一个成员在结构体中的偏移量。

演示样例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define offset(TYPE, MEMBER) ((size_t)(&((TYPE *)0)->MEMBER))
  5.  
  6. int main(){
  7. struct test{
  8. int a;
  9. int b[4];
  10. int c;
  11. };
  12.  
  13. printf("offset a : %lu\n", offset(struct test, a));
  14. printf("offset b : %lu\n", offset(struct test, b));
  15. printf("offset c : %lu\n", offset(struct test, c));
  16.  
  17. struct test *t = (struct test *)0;
  18. printf("%d\n", &(t->c)); // right
  19. printf("%d\n", t->c); //?but cannot do this, core dump
  20.  
  21. return 0;
  22. }

5. s[i] 是 *(s + i) 的语法糖,所以等价于 i[s]。

 演示样例:

  1. #include <stdio.h>
  2.  
  3. int main(){
  4. char s[] = "vonzhou";
  5. printf("%c\n", 2[s]);
  6.  
  7. return 0;
  8. }

6. 再次是printf 和 scanf 的技巧。

  1. #include <stdio.h>
  2.  
  3. int main(){
  4.  
  5. int n = 6;
  6. int val = 1000;
  7. char s[100];
  8. printf("%*d", n, val);// with a minimum width of n,default right aligned
  9. printf("hello\n");
  10.  
  11. //scanf("%*d");//read an integer and ignore it
  12.  
  13. //scanf has regex built into it
  14. //read only selected chars into a string
  15. //scanf("%[abcd]s", s);
  16.  
  17. //read everything excepted heading with abcd
  18. //scanf("%[^abcd]s" ,s);
  19.  
  20. /* some complain that scanf reads upto a whitespace,
  21. so this trick can be used to consume everything upto a newline */
  22. scanf("%[^\n]s", s);
  23. // reads all chars (including whitespaces) till newline is encountered.
  24.  
  25. printf("s = %s\n", s);
  26. /* Another trick with scanf is to consume what is required.
  27. For example - If a user enters his date of birth in YYYY-MM-DD
  28. then you can directly read the year month and date into integer variables */
  29. //scanf("%d-%d-%d", &yy, &mm, &dd);
  30. /* where yy, mm, dd are variables.
  31. Note that this will work only with that format of input.
  32. Anything else like YYYY/MM/DD,and the program will mostly crash.
  33. You specify a hyphen, it expects only a hyphen! */
  34.  
  35. return 0;
  36. }

7.利用scanf的返回值来识别是否到达文件尾EOF;当errno=0时%m格式会输出“Success”;brk(0)能够作为 return 0 的替代;

演示样例:
  1. #include <stdio.h>
  2.  
  3. int main(){
  4. int n;
  5. //util reach EOF(ctrl + D)
  6. while(~scanf("%d",&n)){
  7. //logic process
  8. printf("%d^2 = %d\n", n, n*n);
  9. }
  10.  
  11. // %m print success only if errno = 0
  12. printf("%m\n");
  13.  
  14. brk(0);
  15. }
7.变量会被隐式初始化为0和1;
演示样例:
  1. <pre name="code" class="cpp">#include <stdio.h>
  2.  
  3. // ??
  4. // implicit initiliazation of variables with 0 and 1
  5. g;main(i){
  6.  
  7. printf("%d,%d\n", g, i);
  8.  
  9. return 0;
  10. }
  1.  




C语言中一些非常酷的技巧(cool tricks)的更多相关文章

  1. C语言中的位运算的技巧

    一.位运算实例 1.用一个表达式,判断一个数X是否是2的N次方(2,4,8,16.....),不可用循环语句. X:2,4,8,16转化成二进制是10,100,1000,10000.如果减1则变成01 ...

  2. java语言中一些使用的小技巧(区别于c++)

    正在自学java中...想记录下java和c++在一些小的方面的不同点.(未完待续...) java中class的对象均是引用类型的,如果想把连个同类型的对象相关联起来,只要将一个赋值给另一个就可以了 ...

  3. C语言中的调试小技巧

    C语言中的调试小技巧 经常看到有人介绍一些IDE或者像gdb这样的调试器的很高级的调试功能,也听人说过有些牛人做工程的时候就用printf来调试,不用特殊的调试器.特别是在代码经过编译器一些比较复杂的 ...

  4. php中的include()的使用技巧

    php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...

  5. c++中动态尾随内存的技巧和定位new

    c 和 c++ 最大的特点就是对内存的自由操作,数据类型,其实都是对内存的一种解释方式.C语言中常用的一个技巧就是尾随数据,网络编程中经常会用到这个特性, 特别是以前写完成端口的时候,这个特性肯定是会 ...

  6. C语言中的宏定义

    目录(?)[-] 简单宏定义 带参数的宏 运算符 运算符 宏的通用属性 宏定义中圆括号 创建较长的宏 较长的宏中的逗号运算符 宏定义中的do-while循环do 空操作的定义 预定义宏 C语言中常用的 ...

  7. JavaScript 语言中的 this

    JavaScript 语言中的 this 由于其运行期绑定的特性,JavaScript 中的 this 含义要丰富得多,它可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式.JavaSc ...

  8. php中一些提高性能的技巧

    php中一些提高性能的技巧 tags:php性能 提高性能 php中的@ php的静态 引言:php作为一种脚本语言,本身的性能上肯定是不如c++或者java的.拥有简单易学的特性的同时,性能提升的空 ...

  9. C语言中以字符串形式输出枚举变量

    C语言中以字符串形式输出枚举变量 摘自:https://blog.csdn.net/haifeilang/article/details/41079255 2014年11月13日 15:17:20 h ...

随机推荐

  1. 竹林蹊径-深入浅出Windows内核开发作者的博客

    http://blog.csdn.net/blog_index http://blog.csdn.net/blog_index/article/details/6012054 http://downl ...

  2. tsm ANS0326E问题处理

    备份tsm备份oracle 报错 ANS0326E This node has exceeded its maximum number of mount points. 查看所有节点详细信息 q no ...

  3. 自己动手写处理器之第四阶段(1)——第一条指令ori的实现

    将陆续上传本人写的新书<自己动手写处理器>(尚未出版),今天是第11篇,我尽量每周四篇 第4章 第一条指令ori的实现 前面几章介绍了非常多预备知识,也描绘了即将要实现的OpenMIPS处 ...

  4. MResource

    public class MResource { public static int getIdByName(Context context, String className, String nam ...

  5. HTML5音乐可视化

    环境搭建 1,安装nodejs和Git,配置环境变量2,安装express,npm install -g express-generator3,创建项目,express -e music(项目名称)4 ...

  6. JavaScript 【非IE DOM2级XML】

    DOM2中的XML IE可以实现了对XML字符串或XML文件的读取,其他浏览器也各自实现了对XML处理功能.DOM2级在document.implementaion中引入了createDocument ...

  7. Sublime 学习记录(五) Sublime 其他插件(个人喜好)

    (一)  JSFormat 安装 :命令面板 pci 回车 JSFormat 回车 功能 : javascript的代码格式化插件 简介 : 很多网站的JS代码都进行了压缩,一行式的甚至混淆压缩,这让 ...

  8. (原)工具篇-利用fis压缩项目

    fis3 1.添加 fis-conf.js 到项目根目录中 fis-conf.js 内容如下 : //配置MD5版本控制 fis.match('*.{js,css,png,jpg}', { useHa ...

  9. 查看当前支持的MySQL字符集的命令

    查看不同的MySQL字符集有不同的方法,下面介绍的命令用于查看当前支持的MySQL字符集,希望对您学习MySQL字符集能有所帮助. mysql> show char set; +-------- ...

  10. Android中view的事件

    view:top.left.right.bottom,相对于parent的位置参数,获取通过get*()来获取.width=right-left.height=bottom-top.x=left+tr ...