nerver suppose that a point value is NULL or int value is 0 when defined a variant

964 ワード

when I read some book about C/C++, it's always give a hint that an int variant is assigned to zero automatically . however it doesn't always the tureth
see the test code:
/* by vinco at 2011-08-03
* os : Ubuntu
* compiler :CC for GCC
*/
#include<stdio.h>

int main()
{
        char* str;// = NULL;
        int i;//=0
        if(NULL == str)
                printf("str == NULL
"); else printf("str != NULL
"); if( 0==i ) printf("i == 0
"); else printf("i != 0
"); return 0; }

when compile and run it:
root@vinco:/home/vinco# make  null
cc     null.c   -o null
root@vinco:/home/vinco# ./null
str != NULL
i != 0

you'd better initial the variant firstly once you define it, to make your code more robust or less bug!