Program file name - armstrong3.c.
#include <stdio.h>
#include <math.h>
int main(void) {
int i, sum, num, rem,x;
x=pow(2,5);
printf("%d\n", x);
printf("List of 3 digit armstrong numbers \n");
for (i=100; i<=999; i++) {
num=i;
sum=0;
while (num>0) {
rem=num%10;
sum=sum+pow(rem,3);
num/=10;
}
if (i==sum)
printf("%d\n", i);
}
return 0;
}
This simple program finds 3 digit armstrong numbers. To calculate the cube I am using pow()
of math.h
. It didn't work initially and gave the famous error during compilation:
armstrong3.c:(.text+0x91): undefined reference to `pow' collect2:
error: ld returned 1 exit status.
Then I compiled it with gcc armstrong.c -lm
, and it worked fine.
Then I used another pow()
function in the second line of the main()
function and commented out the earlier pow()
function which I was using to calculate the cube. Strangely, the program compiled fine with gcc armstrong3.c
.
The program posted by me gets compiles fine using gcc armstrong.c -lm
.
The following scenario is my current issue:
With the second pow()
commented out it gcc armstrong.c
compiles with no warnings and errors. No -lm
flag required.
If I uncomment the second pow()
function in the program, it does not compile and gives the above mentioned error. And with -lm
it works fine.
Why is there this unusual behavior with the same function in two places inside the same program?
Compiler: gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04).