Exponents in C

Jinku Hu Oct 12, 2023
  1. Use pow as Exponentiation Function in C
  2. Use Custom Defined Function for Integer Exponentiation in C
Exponents in C

This article will demonstrate multiple methods about how to use exponentiation functions in C.

Use pow as Exponentiation Function in C

The pow function is part of the C mathematical library and is defined in the <math.h> header. The math library should be explicitly linked when using the gcc compiler toolchain.

You should pass -lm flag when compiling or include it in the corresponding build system file as needed. pow is defined for floating-point numbers only; thus, it should not be used with integers for optimal results.

In the following example code, we demonstrate how to calculate the nth exponent of a single double variable. pow takes two parameters - a base number to exponentiate and the exponent itself.

We can chain the result of the pow function into the printf call since it returns the calculated number.

Mind though, there are multiple errors to look out for specific inputs, and all of them are documented on this page.

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

int main() {
  double x = 10.0;
  printf("x: %f\n", x);
  printf("x^2: %f\n", pow(x, 2));

  exit(EXIT_SUCCESS);
}

Output:

x: 10.000000
x^2: 100.000000

Use Custom Defined Function for Integer Exponentiation in C

Alternatively, we can define our custom function to calculate exponentiation for integral numbers.

At first, we implement the function for int values. The implementation is quite straightforward; the iteration with the for loop multiplies the base integer by itself n times. The function returns the calculated int value.

Notice that it does not check for overflow of the integer type, and the user should be aware of this fact when utilizing this function.

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

int exponentInt(const int base, int n) {
  int i, p = base;
  for (i = 1; i < n; ++i) p *= base;
  return p;
}

int main() {
  int x2 = 10;
  printf("x2: %d\n", x2);
  printf("x2^4: %d\n", exponentInt(x2, 4));

  exit(EXIT_SUCCESS);
}

Output:

x2: 10
x2^4: 10000

The previous implementation for the exponentiation function is limited because it can only go as high as 232-1 for calculated numbers as the int type itself is limited with 32-bit storage space.

We can extend this limit with the unsigned long type, which has 64-bit space on corresponding systems. Thus, the exponentiation function’s calculated value could go up to 264-1. Note that this function will overflow after a certain point, as demonstrated in the following example.

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

unsigned long exponentInteger(const unsigned long base, unsigned n) {
  unsigned long i, p = base;
  for (i = 1; i < n; ++i) p *= base;
  return p;
}

int main() {
  int x2 = 10;
  printf("x2: %d\n", x2);
  printf("x2^19: %lu\n", exponentInteger(x2, 19));
  printf("x2^20: %lu\n", exponentInteger(x2, 20));

  exit(EXIT_SUCCESS);
}

Output:

x2: 10
x2^19: 10000000000000000000
x2^20: 7766279631452241920
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C Math