log10() returns the base-10 logarithm of a positive value. It answers questions such as “10 raised to what power gives this number?”
double log10(double x);
#include <math.h>
#include <stdio.h>
int main(void) {
printf("log10(1000) = %.1f\n", log10(1000.0));
return 0;
}
Output
log10(1000) = 3.0
#include <math.h>
#include <stdio.h>
int main(void) {
unsigned int value = 54321;
int digits = (int)floor(log10((double)value)) + 1;
printf("Digits: %d\n", digits);
return 0;
}
This shortcut applies only to positive integers. Handle zero separately if adapting it for general input.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.