Exponentiation in C
This article will demonstrate various ways on how to use exponential functions in C language.
In C language, use pow
as the method of exponential function
pow
The math function is part of the C math library and <math.h>
is defined in the header. When using the C++ gcc
compiler toolchain, the math library should be explicitly linked. -lm
The math flag should be passed when compiling, or included in the appropriate build system file as needed. pow
Only floating point numbers are defined, so for best results it should not be used for integers.
In the following sample code, we demonstrate how to calculate double
the nth power of a single variable. pow
takes two arguments - a base to be exponentialized and the exponent itself. We can pow
chain the result of the function into printf
a call to , since it returns the calculated number. Note, however, that there are multiple errors to watch out for with certain inputs, all of which are documented here .
#include <stdio.h>
#include <stdlib.h>
#include <math.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
Using custom functions for integer exponential calculations in C
Alternatively, we can also define our own custom function to calculate the exponent of an integer. First, we int
implement this function for the value of . The implementation of this function is very straight forward; for
the iterations of the loop multiply the base integer by itself n
times. The function returns the calculated int
value of . Note that it does not check for overflow of integer types, and users should be aware of this fact when using this function.
#include <stdio.h>
#include <stdlib.h>
#include <math.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 of the exponential function is limited in that it can only compute up to 2 32int
-1, since the type is limited to 32 bits of storage. We can unsigned long
extend this limitation by using the type, which has 64 bits of storage on the appropriate system. Therefore, the exponential function can compute up to 2 64 -1. It should be noted that this function will overflow after a certain point, as demonstrated in the following example.
#include <stdio.h>
#include <stdlib.h>
#include <math.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
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Flushing stdout output stream in C
Publish Date:2025/04/17 Views:200 Category:C语言
-
stdout This article will demonstrate various methods on how to flush an output stream in C language . Use function in C language fflush to refresh stdout output stream I/O The C standard library provides a stdio buffered version of the I/O
Using the feof function in C language
Publish Date:2025/04/17 Views:87 Category:C语言
-
feof This article will introduce several ways to use functions in C language . Use feof the function to check the end-of-file indicator on a file stream in C language feof The function is part of the C standard input/output library and is d
Use the C language setenv function to access environment variables
Publish Date:2025/04/17 Views:122 Category:C语言
-
setenv This article will introduce several methods of using functions to export environment variables in C language . setenv Use function to export environment variables in C language Every program running on a Unix-base system has an envir
How to get the size of an array in C
Publish Date:2025/04/17 Views:62 Category:C语言
-
This tutorial explains how to determine the length of an array in C. sizeof() The operator is used to get the size/length of an array. sizeof() Operator determines the size of an array in C sizeof() The operator is a compile time unary oper
Initialize array to 0 in C
Publish Date:2025/04/17 Views:133 Category:C语言
-
This article explains how to initialize an array to 0 in C language. The declaration of an array in C language is as follows. char ZEROARRAY[ 1024 ]; It becomes all zeros at runtime in the global scope. If it is a local array, there is a si
Structure arrays in C
Publish Date:2025/04/17 Views:188 Category:C语言
-
This tutorial explains how to create a structure array in C language, which is a collection of multiple structure variables, each of which contains information about a different entity. Structure arrays in C An array is a sequential collect
Printing Character Array in C
Publish Date:2025/04/17 Views:106 Category:C语言
-
This article will introduce various methods on how to print character array in C language. for How to use loop to print character array in C language If we want to print array elements individually and format the output with more details, f
Clearing a character array in C
Publish Date:2025/04/17 Views:119 Category:C语言
-
This article will introduce several methods to clear character arrays in C language. memset Use function to clear char array in C language memset The _set_storage_region function is commonly used to set a storage area to a constant value. T
Initializing character array in C
Publish Date:2025/04/17 Views:92 Category:C语言
-
This article will demonstrate various ways of initializing a character array in C language. {} Initializing a character array in C using curly brace list notation Character arrays are mostly declared as a fixed-size structure and are often