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




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. 2014第7周1Web安全概念学习

    晚上没有加班,回来后尝试几个感觉不错的行动:1.列出当天最有意义的五件事:2.靠墙站,纠正自己的姿势同时锻炼眼睛:这两点以后也要坚持成为每天的习惯.然后我又陷入了知乎的各种信息中,一个多小时的时间悄悄 ...

  2. codeforces gym 100463I Yawner

    //这题挂得让我怀疑我最近是不是做了什么坏事 题意:一个人有两个集合,先在其中一个集合选一个数x,然后向右走x布,然后再在另一个集合里选一个数y,向左走y步,问是否能走完数轴上所有点. 解:显然是求g ...

  3. linux之SQL语句简明教程---COUNT

    在上一页有提到,COUNT 是函数之一.由于它的使用广泛,我们在这里特别提出来讨论.基本上,COUNT 让我们能够数出在表格中有多少笔资料被选出来.它的语法是: SELECT COUNT(" ...

  4. poj 3287 The Trip, 2007_贪心

    题意:把一个包放入另一个包内,使得总共要带的件数最少,就是说大包可以装小包,且一个大包只能装一个小包,但是这个小包可以继续装更小的包. 思路:因为相同大小的包不能互相装,所以最小数量就是有相同尺寸的包 ...

  5. poj 3254 Corn Fields_状态压缩dp

    感谢:http://www.cnblogs.com/ka200812/archive/2011/08/11/2135607.html 让我搞懂了. #include <iostream> ...

  6. Centos下需安装Pytnon,Pytharm

    1.在www.python.org/PIPY/下载python3.4.2.tar.gz 2.在安装之前最好先安装相关的开发工具 # yum groupinstall develtools # yum ...

  7. IE8对css文件的限制

    很多人在写css时,时常把很多css样式放到一个文件中.也有些框架在上线后,能对很多css文件进行合并.这样能减少对服务器的请求次数,从而加快服务器的响应速度.在IE8中,当css的规则个数大于409 ...

  8. CvMat、Mat、IplImage之间的转换详解及实例

    见原博客:http://blog.sina.com.cn/s/blog_74a459380101obhm.html OpenCV学习之CvMat的用法详解及实例 CvMat是OpenCV比较基础的函数 ...

  9. [AS3]as3用ByteArray来对SWF文件编码加密实例参考

    [AS3]as3用ByteArray来对SWF文件编码加密实例参考,简单来说,就是将 swf 以 binary 的方式读入,并对 ByteArray 做些改变,再重新存成 swf 档.这个作业当然也可 ...

  10. R-plot

    颜色.图例和线 在散点图中添加信息.图例以及回归线. 模拟数据 #模拟数据 dat <- data.frame(X = runif(100,-2,2),T1 = gl(n=4,k=25,labe ...