C log()

The log() function defined in the math.h header file. It helps to return the natural logarithm (base-e logarithm) of the given argument value.


double log(double x); #where x should be in double

Also, two functions logf() and logl() were used with type float and long double respectively.


float logf(float x); 
long double logl(long double x); 

log() Parameters:

The log() function takes a single parameter in double.

Parameter Description Required / Optional
x whose natural logarithm needs to be found Required

log() Return Value

The return value of log() function is a number in float.

Input Return Value
x > 0 Finds the log of the argument
x < 0 Shows run-time error

Examples of log() 

Example 1: Working of log() function in C?


#include <stdio.h>
#include <math.h>
int main()
{
    double N = 5.6, output;

    output = log(N);
    printf("The value of log(%.1f) is %.2f", N, output);

    return 0;
}

Output:


The value of log(5.6) is 1.72

Example 2: How log() function worked in C?


#include <stdio.h>
#include <math.h>
int main () {
   double k, out;
   k = 2.7;
   out = log(k);
   printf("The value of log(%lf) is %lf", k, out);
   return(0);

}

Output:


The value of log(2.700000) = 0.993252