码迷,mamicode.com
首页 > 编程语言 > 详细

C语言中一些很酷的技巧(cool tricks)

时间:2014-07-12 19:30:20      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:printf   scanf   regex   

来自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),布布扣,bubuko.com

C语言中一些很酷的技巧(cool tricks)

标签:printf   scanf   regex   

原文地址:http://blog.csdn.net/vonzhoufz/article/details/37724193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!