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\n");
else
printf("str != NULL\n");
if( 0==i )
printf("i == 0\n");
else
printf("i != 0\n");
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!
本文通过一个简单的C/C++示例程序说明了为何不能依赖未初始化的变量默认为零,并强调了显式初始化的重要性。文章指出,在某些情况下,不初始化的变量可能导致不可预测的行为,从而增加代码的潜在错误。
304

被折叠的 条评论
为什么被折叠?



