The pow()erful Function

When I first learned the C language, I was surprised to find something missing from its assortment of operators. The +, -, *, and / operators are pretty common for nearly all programming languages. And you’ll find the % and ! operators used for modulus and logical NOT in a few programming languages. Yet what other languages have that C lacks is an exponent or power operator.

In other languages, I recall the ^ operator was the power operator. In BASIC, for example, you could write the following expression:

2^10

Which means 210 or two to the tenth power, 1024. In the C language, however, ^ is the exclusive OR (or XOR) operator.

It makes sense that the C language prioritizes an bitwise operator for ^ instead of using it as a power operator: C is a mid-level programming language. That doesn’t imply that you can’t raise a value to a specific power. The tool to perform that operation is the pow() function.

Here is the pow() function’s man page definition:

double pow(double x, double y)

x is the base and y is the exponent. The values are all doubles, which makes sense because the results could be incredibly huge or infinitesimally small.

The pow() function is declared in the math.h header file.

The following code uses the pow() function to display powers of 2 from 1 through 10, which are coincidentally the first ten holy computer numbers:

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

int main()
{
    double t;

    for(t=1.0;t<=10.0;t++)
        printf("2 to the %.f power is %.f\n",
                t,
                pow(2.0,t));

    return(0);
}

Line 6 declares variable t to be a double, which is required for the pow() function.

The for loop at Line 8 uses double values, 1.0 and 10.0 for variable t. Remember that floating point values (literals) in C must be specified with a decimal part. So values 1.0 and 10.0 are used where you’d otherwise set integers.

The pow() function appears within the printf() statement, which is split between Lines 9, 10, and 11. The double constant 2.0 is specified directly in the function. Also, the printf() function uses the %.f placeholder to ensure that the output contains only the whole number portion of the answer.

Here is the code’s output:

2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512
2 to the 10 power is 1024

If you replace the %.f placeholder with %f (which might be your first inclination), the output looks like this:

2 to the 1.000000 power is 2.000000
2 to the 2.000000 power is 4.000000
2 to the 3.000000 power is 8.000000
2 to the 4.000000 power is 16.000000
2 to the 5.000000 power is 32.000000
2 to the 6.000000 power is 64.000000
2 to the 7.000000 power is 128.000000
2 to the 8.000000 power is 256.000000
2 to the 9.000000 power is 512.000000
2 to the 10.000000 power is 1024.000000

That’s a bit confusing, so you can see why I instead specified %.f in the printf() statement.

Leave a Reply