我不明白为什么if以下声明的结果总是不正确的:
unsigned long is_linux;
printf("plz. enter a digit : ");
gets(str);
sscanf(str,"%d",&is_linux);
printf("the value of is_linux = %d \n",is_linux);
if(is_linux==1)
printf("Here is 1 ! \n");
else
printf("There is 0 ! \n");
Run Code Online (Sandbox Code Playgroud)
我只能得到"有0!" 声明,即使我输入"1".此外,printf()语句is_linux正确显示(1)的值:
Run Code Online (Sandbox Code Playgroud)plz. enter a digit : 1 the value of is_linux = 1 There is 0 !
例如,如果我将变量更改为其他名称,则效果很好test.有没有理由不使用is_linux变量?我用gccat 编译了上面的源代码x86_64-redhat-linux.
在你的代码中
sscanf(str,"%d",&is_linux);
Run Code Online (Sandbox Code Playgroud)
导致未定义的行为.
is_linux是类型unsigned long,它要求转换说明符lu.
将错误(不匹配)类型的参数传递给任何转换说明符会导致未定义的行为.