What is log() in C?

The log function is a C library function that returns the natural logarithm of an argument.

To use the log function, the math header file needs to be included in the program, as follows:

#include <math.h>

Parameters

The log function takes a single argument of the floating-point type and computes its logarithm to the base of e.

Return Value

If the argument is greater than zero, the log function returns the natural logarithm of the argument in the float type.

  • If the argument is 0, a range error is returned

  • If the argument is less than zero, a domain error is returned

Example

The following code explains the use of log function in C:

#include <stdio.h>
#include <math.h>
int main() {
//Define argument 'x'
double x = 5.1;
//Compute the Log
double answer;
answer = log(x);
//Display the result
printf ("The Log of %f is %f", x, answer);
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved