## Working of Power Function in C

The pow() function accepts double values as parameters and returns a double value. However, the function does not always operate with integers.

For example, if the value given by pow(5, 4) is assigned to an integer variable, the result may be 624 on certain compilers and 625 on others. This is because the value 5^4 (i.e. 625) might be saved as 624.999999 in certain compilers and 625.0000000001 in others. When these numbers are given to int, 624.999999 translates to 625, resulting in erroneous output.

We can overcome this problem by adding 0.0000000001 (1^{-9} or 1e-9) to the output and explicitly typecasting it into an int. For example, (int) (pow(4, 3) + 1e-9) will always give the correct answer. You can learn more about Typecasting in C from this link.

You can also read about __C dynamic array__ and __Short int in C Programming__

**Syntax for Pow() Function in C**

The syntax of the pow() function is as follows:

`double pow(double x, double y);`

The pow() function takes two integer arguments x and y.

x: The variable x represents the base value, whose power needs to be calculated. It is of the type double.

y: The variable y represents the exponent value. It is of the double type.

Return Values for pow() Function in C

The pow() function returns the value x^y(x raised to the power of y) where x and y are two variables of type double. The return type is double.

You can also read about the __jump statement____, ____Tribonacci Series__

Must Read __Passing Arrays to Function in C__

**Parameters for pow() in C**

The pow() function in C takes two parameters: the base value (double) and the exponent value (double) to calculate the power.

**Return Values for pow() Function in C**

The pow() function returns the result of raising the base to the power of the exponent as a double precision floating point number.

## Examples for pow() in C

#### Example 1

Let's take an example. To better understand the pow() function, consider the following scenario: The base value is an integer, and the power value is a double.

### C

`#include <stdio.h>`

#include <math.h>

int main()

{

int x = 5; // base

double y = 6.1; // power

// using the pow() function

double ans = pow(x, y);

// printing the output

printf("%f", ans);

return 0;

}

**Output**

`18353.420986`

**Explanation**

In the above example, the base was of type int while the power was of type double. We used pow() on these variables to get our output.

#### Example 2

Let's look at an example to see how we can use the pow function to simply get the square root of an integer. A number's square root is just that number raised to the power of Â½ or 0.5.

### C

`#include <math.h>`

#include <stdio.h>

int main()

{

printf("%f", pow(9, 0.50));

return 0;

}

Output:

`3.000000`

**Explanation**

pow() can be used to calculate the roots of positive numbers.

#### Example 3

Let's look at an example to see how we can pass negative values with the power of type int.

### C

`#include <math.h>`

#include <stdio.h>

int main()

{

printf("%f \n", pow(-2, -7));

printf("%f", pow(2, -7));

return 0;

}

**Output**

```
-0.007812
0.125000
```

**Explanation**

The power's value in pow() can be negative.

#### Example 4

Let's look at an example of passing negative values with the power of type double.

### C

`#include <math.h>`

#include <stdio.h>

int main()

{

printf("%f \n", pow(-3.5, 11.2));

printf("%f", pow(-5.5, -7.2));

return 0;

}

**Output**

```
-nan
-nan
```

**Explanation**

If the base is negative and the power is of type double, the output will be -nan as it is an undefined entity. If we calculate the root of a negative number using pow(), we will get -nan as the output, which means it is an undefined value as the root of a negative number is not a real number. For better understanding you can practice by yourself on the online compiler.

Must Read** **__Decision Making in C__

## Frequently Asked Questions

### What is the use of the pow() function?

pow() function returns the base to the exponent power, as in base^exponent , the base and the exponent are in a decimal numeral system.

### Which header file is used for the pow() function?

In order to use pow(), we must include the math.h library in our application. The base value (x) and the exponent value are both sent to the pow() method.

### How do I use the pow() function in C to calculate the power of a number?

To use the pow() function in C for calculating the power of a number, include the math.h header file, then call the pow() function with the base value and exponent as parameters. For example, pow(2, 3) calculates 2 raised to the power of 3, resulting in 8.

### What happens when a negative base and a non-integer exponent are passed into the pow() function?

In general, pow(x,y) does not exist in the real numbers for the negative base and non-integer exponent. For positive x, however, pow(x,y) always exists and is real.

## Conclusion

In this article, we took a look at the pow() function in C and also learned its operation. The pow() function in C offers a straightforward solution for calculating powers of numbers. By including the math.h header file, developers can leverage this function to efficiently compute exponentiation in their programs.

**Recommended Articles**