log C Library Function

The function double log(double x); returns the natural logarithm of x. The natural logarithm is the base-e logarithm.

Function prototype of log

double log(double x);
  • x : A floating point value whose logarithm to be calculated. It must be positive.

Return value of log

It returns the natural logarithm of x. If x is negative, a domain error occurs.

C program using log function

The following program shows the use of log function.

log C Library Function

#include <stdio.h>
#include <math.h>
 
int main ()
{
  double x, result;
  /* Logarithm is only defined for positive numbers */
  printf("Enter a positive number\n");
  scanf("%lf", &x);
   
  result = log(x);
  printf("Logarithm of %lf with base e is : %lf", x, result);
   
  return 0;
}

Output

Enter a positive number
5.6
Logarithm of 5.600000 with base e is : 1.722767