pow() Function

The pow() function in C computes the result of raising a number to a specified power.


Syntax of pow()

</>
Copy
double pow(double base, double exponent);

Parameters

ParameterDescription
baseThe number to be raised.
exponentThe power to which the number is raised.

It is important to note that if a domain error occurs (for example, when the base is a finite negative number and the exponent is not an integer), the global variable errno is set to EDOM. Similarly, if a pole or range error occurs, errno may be set to ERANGE.


Examples for pow()

Example 1: Calculating a Positive Exponential Value

This example demonstrates how to compute a number raised to a positive power using pow().

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double result = pow(2.0, 3.0);
    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. The function calculates 2.0 raised to the power 3.0.
  2. The computed value (8.0) is stored in the variable result.
  3. The result is printed, displaying the output.

Program Output:

Result: 8.000000

Example 2: Computing a Negative Exponent

This example shows how pow() handles negative exponents, which yield the reciprocal of the base raised to the corresponding positive power.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double result = pow(2.0, -2.0);
    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. The function calculates 2.0 raised to the power -2.0.
  2. This computes the reciprocal of 2.0 squared (i.e., 1/(2.0²)).
  3. The computed value (0.25) is stored and printed.

Program Output:

Result: 0.250000

Example 3: Evaluating Fractional Exponents for Square Roots

This example illustrates the use of pow() with a fractional exponent to compute the square root of a number.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double result = pow(9.0, 0.5);
    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. The function calculates 9.0 raised to the power 0.5.
  2. This effectively computes the square root of 9.0.
  3. The resulting value (3.0) is printed as the output.

Program Output:

Result: 3.000000