This is my basic C test program. After I built it, I just entered negative number like -1
, -2
, etc. in console. But the result is "oh", not "another number". I don't know why this happens, because negative numbers should make the 'if' statement true.
这是我的基本C测试程序。在我构建它之后,我只是在控制台中输入负数,如-1,-2等。但结果是“哦”,而不是“另一个数字”。我不知道为什么会发生这种情况,因为负数应该使'if'语句成立。
int main(int argc, char* argv[]){
long int num;
scanf("%d", &num);
if(num ==1 || num < 0){
printf("another number\n");
}else{
printf("oh\n");
}
}
3 个解决方案
#1
2
When you use the %d
format string with scanf, the corresponding argument will be treated as int*
. But you have passed a long int*
. The value scanf
stores will not be the same size as what your if
statement reads.
将%d格式字符串与scanf一起使用时,相应的参数将被视为int *。但是你已经通过了一个很长的int *。值scanf存储的大小与if语句读取的大小不同。
Formally, you get undefined behavior. In practice, on most platforms scanf
will write only part of the variable, and the rest will be left with an arbitrary value, with the usual bad effects on future use.
在形式上,您会得到未定义的行为。实际上,在大多数平台上,scanf只会写入变量的一部分,其余的将保留一个任意值,通常会对将来的使用造成不良影响。
#2
2
Use %ld
for long
variables, %d
for int
s. Change your code to one of these:
对于长变量使用%ld,对于int使用%d。将您的代码更改为以下之一:
int num;
scanf("%d", &num);
or
long int num;
scanf("%ld", &num);
#3
1
/tmp$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:4:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
foo.c:4:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
foo.c:4:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘long int *’ [-Wformat]
foo.c:7:9: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
foo.c:7:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:9:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
fix the causes of those warnings and all the bugs will go away.
修复这些警告的原因,所有的错误都会消失。
#1
2
When you use the %d
format string with scanf, the corresponding argument will be treated as int*
. But you have passed a long int*
. The value scanf
stores will not be the same size as what your if
statement reads.
将%d格式字符串与scanf一起使用时,相应的参数将被视为int *。但是你已经通过了一个很长的int *。值scanf存储的大小与if语句读取的大小不同。
Formally, you get undefined behavior. In practice, on most platforms scanf
will write only part of the variable, and the rest will be left with an arbitrary value, with the usual bad effects on future use.
在形式上,您会得到未定义的行为。实际上,在大多数平台上,scanf只会写入变量的一部分,其余的将保留一个任意值,通常会对将来的使用造成不良影响。
#2
2
Use %ld
for long
variables, %d
for int
s. Change your code to one of these:
对于长变量使用%ld,对于int使用%d。将您的代码更改为以下之一:
int num;
scanf("%d", &num);
or
long int num;
scanf("%ld", &num);
#3
1
/tmp$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:4:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
foo.c:4:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
foo.c:4:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘long int *’ [-Wformat]
foo.c:7:9: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
foo.c:7:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:9:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
fix the causes of those warnings and all the bugs will go away.
修复这些警告的原因,所有的错误都会消失。