I don't understand why the result of the if
statement below is always incorrect:
我不明白为什么下面的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");
I can only get "There is 0 !" statement, even if I entered "1". Furthermore, printf()
statement displays the value of is_linux
(1) correctly:
我只能得到“有0!”声明,即使我输入“1”。此外,printf()语句正确显示is_linux(1)的值:
plz. enter a digit : 1 the value of is_linux = 1 There is 0 !
It works well if I change the variable to another name, for instance,test
. Is there any reason for not using is_linux
variable ? I've compiled above source code using gcc
at x86_64-redhat-linux
.
如果我将变量更改为其他名称(例如test),它的效果很好。有没有理由不使用is_linux变量?我在x86_64-redhat-linux上使用gcc编译了上面的源代码。
4 个解决方案
#1
5
In your code
在你的代码中
sscanf(str,"%d",&is_linux);
causes undefined behavior.
导致未定义的行为。
is_linux
is of type unsigned long
, it demands the conversion specifier to be lu
.
is_linux的类型为unsigned long,它要求转换说明符为lu。
Passing wrong (mismatched) type of argument to any conversion specifier causes undefined behavior.
将错误(不匹配)类型的参数传递给任何转换说明符会导致未定义的行为。
#2
0
No. You should be able to use the variable name. is_linux
is not reserved and it's not a keyword. Worst-case scenario, it could (but shouldn't) be a macro (linux
commonly is, unfortunately). Preemptively, or if you get compilation errors because it is a macro on your platform, you could #undef
it first. (Make sure the rest of your code is correct.)
不可以。您应该能够使用变量名称。 is_linux不是保留的,也不是关键字。在最坏情况下,它可能(但不应该)是一个宏(不幸的是,linux通常是这样)。抢先一步,或者如果因为它是您平台上的宏而遇到编译错误,您可以先#undef它。 (确保其余代码正确无误。)
#3
0
The datatype you are assigning to is_linux is not compatible. You set it as a long, but giving it an int so when you check with your if statement you are checking for a long value, not int.
您分配给is_linux的数据类型不兼容。你将它设置为long,但是给它一个int,所以当你检查你的if语句时,你要检查一个long值,而不是int。
#4
0
Thank you so much, there. All comments were very helpful to me for finding root cause. Actually, it was very simple reason, data type. Simply, I changed %d to %lu in both of sscanf and printf and got correct answer.
非常感谢你,那里。所有评论都对我找到根本原因非常有帮助。实际上,这是非常简单的原因,数据类型。简单地说,我在sscanf和printf中都将%d更改为%lu并得到了正确的答案。
My machine was 64bit lunux system and it treated unsigned long as 8-byte unit. And also, I misunderstood %d could cover all the integer data types. Anyway, I'm very appreciate all you guy's comments. Thank you :)
我的机器是64位lunux系统,它将无符号长度视为8字节单位。而且,我误解了%d可以覆盖所有整数数据类型。无论如何,我非常感谢你们所有人的评论。谢谢 :)
#1
5
In your code
在你的代码中
sscanf(str,"%d",&is_linux);
causes undefined behavior.
导致未定义的行为。
is_linux
is of type unsigned long
, it demands the conversion specifier to be lu
.
is_linux的类型为unsigned long,它要求转换说明符为lu。
Passing wrong (mismatched) type of argument to any conversion specifier causes undefined behavior.
将错误(不匹配)类型的参数传递给任何转换说明符会导致未定义的行为。
#2
0
No. You should be able to use the variable name. is_linux
is not reserved and it's not a keyword. Worst-case scenario, it could (but shouldn't) be a macro (linux
commonly is, unfortunately). Preemptively, or if you get compilation errors because it is a macro on your platform, you could #undef
it first. (Make sure the rest of your code is correct.)
不可以。您应该能够使用变量名称。 is_linux不是保留的,也不是关键字。在最坏情况下,它可能(但不应该)是一个宏(不幸的是,linux通常是这样)。抢先一步,或者如果因为它是您平台上的宏而遇到编译错误,您可以先#undef它。 (确保其余代码正确无误。)
#3
0
The datatype you are assigning to is_linux is not compatible. You set it as a long, but giving it an int so when you check with your if statement you are checking for a long value, not int.
您分配给is_linux的数据类型不兼容。你将它设置为long,但是给它一个int,所以当你检查你的if语句时,你要检查一个long值,而不是int。
#4
0
Thank you so much, there. All comments were very helpful to me for finding root cause. Actually, it was very simple reason, data type. Simply, I changed %d to %lu in both of sscanf and printf and got correct answer.
非常感谢你,那里。所有评论都对我找到根本原因非常有帮助。实际上,这是非常简单的原因,数据类型。简单地说,我在sscanf和printf中都将%d更改为%lu并得到了正确的答案。
My machine was 64bit lunux system and it treated unsigned long as 8-byte unit. And also, I misunderstood %d could cover all the integer data types. Anyway, I'm very appreciate all you guy's comments. Thank you :)
我的机器是64位lunux系统,它将无符号长度视为8字节单位。而且,我误解了%d可以覆盖所有整数数据类型。无论如何,我非常感谢你们所有人的评论。谢谢 :)