C log10() Function

Definition
In C, log10() returns the base-10 logarithm of x. log10(100) is 2, and log10(1) is 0.
You pass one double, and it must be greater than 0. The result is a double. Include <math.h>.
Syntax
Pass a number greater than 0. Start with this call.
| Call | What it does |
|---|
log10(100.0) | Returns 2, because 10² is 100. |
Include <math.h>. On GCC, link with -lm. For the natural log, use log().
Library declarations
double log10(double x);
float log10f(float x);
long double log10l(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. A double greater than 0. |
Return Value
| Return Value | Description |
|---|
| Base-10 log | A double. log10(100) is 2. |
| NaN | Returned when x is negative. |
C Examples
Example 1 — Basic Common Logs
Landmark powers of ten return clean integers.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("log10(1) = %.1f\n", log10(1.0));
printf("log10(10) = %.1f\n", log10(10.0));
printf("log10(1000) = %.1f\n", log10(1000.0));
return 0;
}
log10(1) = 0.0
log10(10) = 1.0
log10(1000) = 3.0
How It Works
- Include math.h. That declares
log10. - log10(1) is 0. Because
10^0 = 1. - log10(10) is 1. One power of ten.
- log10(1000) is 3. Three powers of ten (
10^3).
Example 2 — Count Digits of a Positive Integer
For an integer n > 0, the digit count is floor(log10(n)) + 1.
#include <stdio.h>
#include <math.h>
int main(void) {
int n = 12345;
int digits = (int)floor(log10((double)n)) + 1;
printf("n = %d\n", n);
printf("digits = %d\n", digits);
return 0;
}
How It Works
- Convert to double.
log10 needs a floating argument. - Take log10.
log10(12345) is a bit over 4. - Floor then +1.
floor gives 4; add 1 for five digits. - Only for n > 0. Zero and negatives need separate handling.
Example 3 — Inverse With pow()
Raising 10 to log10(x) recovers x (within floating-point error).
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 250.0;
double t = log10(x);
double back = pow(10.0, t);
printf("x = %.6f\n", x);
printf("log10(x) = %.6f\n", t);
printf("pow(10, t) = %.6f\n", back);
return 0;
}
x = 250.000000
log10(x) = 2.397940
pow(10, t) = 250.000000
How It Works
- Find the exponent.
log10(250) is about 2.398. - Raise 10 again.
pow(10.0, t) rebuilds the original scale. - Round-trip. You get back ~250.
- Pair for base 10. Use
log10 + pow(10, …), not exp/log, when thinking in decades.
Frequently Asked Questions
log10() returns the base-10 logarithm of x. log10(100) is 2, and log10(1) is 0.
x is required. It must be a double greater than 0.
A double. A negative x returns NaN, and log10(0) goes toward negative infinity.
Include <math.h>. On GCC, link with -lm.
log10() is base 10. log() is the natural logarithm, base e.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful