how to find the correct type for largest number ?
#include <stdio.h>
/**
* factor_prime - prints the prime factors of a number
* @n: number
*/
void factor_prime(unsigned long long n)
{
long i;
long inter = n;
printf("n : %lld\n", n);
for (i = 2; i <= n; i++)
{
if (n % i == 0)
{
n = n / i;
printf("%ld=%lld*%ld\n", inter, n, i);
return;
}
}
}
int main(void)
{
factor_prime(1718944270642558716715u);
}
output : 3397071787570416427=35568825191561*95507
expected : 1718944270642558716715=343788854128511743343*5
how to fix ?
You are using a too big integer constant that can not be represented in any object of an integer type.
Consider this demonstration program.
#include <stdio.h>
Its output is
The outputted constant contains only
20digits while your constant1718944270642558716715uthat you are trying to use contains22digits.Pay attention to that in any case your function is incorrect. The function parameter has the type
unsigned long longthat you are assigning to a variable of the signed typelongAs you are trying to pass a very big number then the assignment results in getting an invalid value.