C Programming

Pow Function in C

The Power Function is a standard C function in C programing language that computes the power upraised to the base value. It is defined in the C library’s header file ” math.h “. If we have two numbers, one of which is the base and the other is an exponent. The POW () function is being used to find the value of a raised to the power of b, which is ab . For example, the Pow () formula is used to determine the value of ab. It needs two arguments, power number, and base value, which are used to calculate the power of values.

Return Value and Error handling

Finding the power of a number is a relatively common programming procedure. If no problems occur, baseexp that is base raised towards the power of exponent is provided. If there is an underflow problem, the right result after rounding is reported. A domain problem and a range error can arise when the base is limited and negative and the exponent is limited and non-integer. A domain mistake can arise if the base and exponent are both zero. Now, let’s start with the implementation of the POW () function in the C programming language.

Example 1

Open the GCC compiler of Windows 10 and create a new file. Add the appended below code into it. The name of our file is “Untitled1.c”. The extension .c depicts that it has some C language code.

In the above presented code, we have defined header libraries that are necessary for program execution. After that, we have declared three different variables to get the value of base, and power. The variables are “b”, “p”, and “r”. One of the variables is utilized to store the result. This example code will take the base and exponent value from the user and save its value in the “r” variable. The output has been printed by using the printf() function. Once the code is completed, save the file and compile it by selecting the “Compile and Run” option present in the GCC compiler. The compilation will display the black console screen. Initially, the user has to enter the base value as requested.

Once it is entered, you have to enter the exponent value. After that, pow() function will perform its functionality and the output will appear on the screen.

Example 2

This example is somehow different from the previous example. Open the GCC compiler of Windows 10 and create a new file. Add the appended below code into it. The earlier file can also be used for this purpose. The name of our file is “Untitled1.c”. The extension .c illustrates that it has some C language code.

In the above presented code, we have defined header libraries that are necessary for program execution. After that, we have declared three different variables. One of the variables is utilized to store the result and we named it “power”. This example code will not take base and exponent values from the user at run time because the values have been declared in variables “a” and “b”.

The output has been printed by using the printf() function. Once the code is completed, save the file and compile it by selecting the “Compile and Run” option present in the GCC compiler. The compilation will display the black console screen. After that, pow() function will complete its functionality and the output will appear on the screen.

Example 3

Now, we have another example that is unique from both examples explained in the previous examples of this article.  Open the GCC compiler of Windows 10 and create a new file. Add the appended below code into it. The previous file can also be used for this purpose. The name of our file is “Untitled1.c”. The extension .c describes that it has some C language code.

In the above presented code, we have defined header libraries that are necessary for program execution. After that we have declared one variable title as “x” and its data type is an integer. The base and exponent values have been added in the pow () function with the addition of another value. The output has been printed by using the printf() function. Once the code is completed, save the file and compile it by selecting the “Compile and Run” option present in the GCC compiler.

The compilation will display the black console screen. After that, pow () function will complete its functionality and the output will appear on the screen. This program not only displays the power but also adds value to it as well.

Conclusion

This article provided a brief introduction to power function in the C programming language. We have described some common errors along with the return value of this function. Three different examples have been elaborated and implemented in the GCC compiler, the illustrations can be altered as per your working needs. Now, I expect after implementing and understanding this whole guide, the user will be able to understand the power function and calculate their desired value.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content