Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
pow() function in C
The pow() function in C is used to calculate the power of a number. It computes base raised to the power of exponent and returns the result as a double. This function is declared in the math.h header file.
Syntax
double pow(double base, double exponent);
Parameters
- base − The base value whose power is to be calculated
- exponent − The power value (exponent)
Return Value
Returns the value of base raised to the power of exponent as a double.
Example 1: Basic Usage
Here is a simple example demonstrating the pow() function −
#include <stdio.h>
#include <math.h>
int main() {
double base = 5.5;
double exponent = 4.0;
double result;
result = pow(base, exponent);
printf("%.1f raised to the power %.1f = %.6f<br>", base, exponent, result);
return 0;
}
5.5 raised to the power 4.0 = 915.062500
Example 2: Multiple Power Calculations
This example shows various power calculations using different data types −
#include <stdio.h>
#include <math.h>
int main() {
printf("2^3 = %.0f<br>", pow(2, 3));
printf("2.5^2 = %.2f<br>", pow(2.5, 2));
printf("10^-2 = %.4f<br>", pow(10, -2));
printf("4^0.5 = %.2f<br>", pow(4, 0.5));
return 0;
}
2^3 = 8 2.5^2 = 6.25 10^-2 = 0.1000 4^0.5 = 2.00
Note: On some systems, you may need to link the math library using
-lmflag during compilation:gcc program.c -lm
Key Points
- Both parameters are of type
double, but integers are automatically promoted - The function can handle negative exponents and fractional powers
- For square root, use
pow(x, 0.5)or the dedicatedsqrt()function - May require linking with math library (
-lm) on some compilers
Conclusion
The pow() function provides a convenient way to perform power calculations in C. It handles various numeric types and supports both positive and negative exponents for flexible mathematical operations.
