I'm working on 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. I'm current trying to use %li and this is the error I'm getting:
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("%li", &greatest);
As another answer shows as well, you need to use %lu, as you're using an unsigned long int:
scanf("%lu", &greatest);
Use %lu format, as it represents an unsigned long int instead of just a long int
scanf("%lu",&greatest);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With