I was reading "The C programming language" by Dennis Ritchie and had a doubt in the following code for finding value of base raised to n-th power:
#include <stdio.h>
/* test power function */
int power(int m, int n);
main()
{ int i;
for (i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;
}
/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
//Code causing confusion
p = p * base;
return p;
//end
}
This code was working fine with the following output.
0 1 1
1 2 -3
2 4 9
3 8 -27
4 16 81
5 32 -243
6 64 729
7 128 -2187
8 256 6561
9 512 -19683
My doubt was that p is set to 1 yet why is p = p * base printing the values?