The following function is supposed to be used to count the number of digits in a number. Unfortunately it doesn't work for numbers with 11 or more digits and I'm not sure why. I feel like it has something to do with the data type but I thought long long int would be fine in this case. Thanks for the help!
long long int getLength(long long int input)
{
long long int length = 0;
while(input != 0)
{
input /= 10;
++length;
}
return (length);
}
转载于:https://stackoverflow.com/questions/53111930/digit-count-function-for-c-programming-wont-work-past-11-digits
This may not stand as an answer, but allow me to report output here. Can you double check? It works on my side.
#include "stdio.h"
long long int getLength(long long int input)
{
long long int length = 0;
while(input != 0)
{
input /= 10;
++length;
}
return (length);
}
int main()
{
printf("%lld\n", getLength(12345678901)); // 11
printf("%lld\n", getLength(123456789012)); // 12
printf("%lld\n", getLength(1234567890123)); // 13
printf("%lld\n", getLength(0)); // 0
printf("%lld\n", getLength(-123)); // 3
}
Platform Windows 10, and gcc --version
returns
gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.3.0
Question 1: do you really need type long long int
to report number of digits?
Question 2: did you correctly use the format specifier %lld
in your printf
?