What is the Power Function in C?
The power function in C is used to raise a number (called the base) to the power of another number (called the exponent). In simple terms, it computes:
pow(base, exponent) = base ^ exponent
For example:
pow(2, 3) = 2 × 2 × 2 = 8
Syntax of pow() Function
To use the pow() function, include the math.h header file in your program:
#include <math.h>
The function prototype is:
double pow(double base, double exponent);
- base – the number to be raised.
- exponent – the power to which the base is raised.
Example of Power Function in C
#include <stdio.h> #include <math.h> int main() { double quizMarks = 3.0; double attempts = 4.0; double totalScore = pow(quizMarks, attempts); // 3^4 = 81 printf("C Programming Score: %.2f\n", totalScore); return 0; }
Output:
C Programming Score: 81.00
This C program calculates a score using the pow() function from the
Important Notes:
- Always include math.h or you’ll get an “implicit declaration” warning.
- Compile with -lm if using GCC:
- Return value is of type double, so you may need to cast it if you want an integer.
- If you pass a negative base with a non-integer exponent, the result is undefined (NaN).
gcc program.c -o program -lm
int result = (int)pow(2, 3); // result = 8
pow() Function with Different Data Types
Although pow() accepts and returns double, you can use it with integers by typecasting:
int base = 5, exp = 2; int result = (int)pow((double)base, (double)exp); // result = 25
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check Computer Science Books
- Apply for C Internship
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos