[英]C scanf format string warning
I'm working on Euler #3 ( http://projecteuler.net/problem=3 ). 我正在研究Euler#3( http://projecteuler.net/problem=3 )。 I think I've got the logic right, but I'm getting an error when trying to use scanf (and printf) with a long. 我认为我的逻辑是正确的,但是在尝试使用scanf(和printf)时遇到错误。 I'm current trying to use %li and this is the error I'm getting: 我目前正在尝试使用%li,这是我得到的错误:
euler3.c: In function ‘main’:
euler3.c:30: warning: format ‘%li’ expects type ‘long int **’, but argument 2 has type ‘long int’
euler3.c:30: warning: format ‘%li’ expects type ‘long int *’, but argument 2 has type ‘long int’
I understand the error, but for the life of me I can't find the solution. 我理解错误,但对于我的生活,我找不到解决方案。 Here's my code if it's needed. 如果需要,这是我的代码。
#include <stdio.h>
long greatestPrime(long num)
{
int i;
for(i = 2; i <= num; i++)
{
if(num%i == 0)
{
num = num/i;
i--;
}
}
return num;
}
int main(int argc, char *argv[])
{
unsigned long greatest;
printf("Enter number to find prime factor: ");
scanf("%li",greatest);
printf("%li",greatestPrime(greatest));
return 0;
}
scanf
is looking for a pointer to a long integer ( long int *
), not a long integer, so you need to pass the address of greatest
by using the &
operator: scanf
正在寻找一个指向长整数( long int *
)的指针,而不是长整数,所以你需要使用&
运算符传递greatest
的地址:
scanf("%li", &greatest);
As another answer shows as well, you need to use %lu
, as you're using an unsigned long int
: 正如另一个答案所示,您需要使用%lu
,因为您使用的是unsigned long int
:
scanf("%lu", &greatest);
使用%lu
格式,因为它表示unsigned long int
而不是long int
scanf("%lu",&greatest);
In order for scanf
to modify your variable, it needs the address of it (a pointer to your variable). 为了让scanf
修改你的变量,它需要它的地址(指向你变量的指针)。 Pass the address of greatest
using the &
operator: 使用&
运算符传递greatest
的地址:
scanf("%lu", &greatest);
EDIT : Also, %li
should be %lu
, since greatest
is unsigned. 编辑 :此外, %li
应该是%lu
,因为greatest
是无符号。
You're passing an integer to scanf()
: 你将一个整数传递给scanf()
:
scanf("%li",greatest);
You should be passing the address of a variable properly typed to hold an unsigned long integer: 您应该传递正确键入的变量的地址以保存无符号长整数:
scanf("%lu", &greatest);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.