The following code finds the square root of a number, it runs fine unless you compile with MinGW gcc:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
double x0,x1,a;
int i;
x1 = 1;
x0 = 0;
printf("\nFind square root of: ");
scanf("%lf",&a);
for(i = 0; x0 != x1; ++i)
{
x0 = x1;
x1 = .5 * (x1 + a / x1);
// printf("\nIteration %d - converging to %lf - prev value %lf",i,x1,x0);
}
printf("Finished");
return 0;
}
The problem is if compiled with compilers other than gcc (I have tested it with lcc, visual C++ and borland C++) it reaches "finished" but for gcc it never gets out of the loop. Try to find sqrt of 5, 7 and you will see what I mean. I know about the issues involving comparison of double datatypes, but in this particular case it is guaranteed that x1 will stop changing after certain iterations.
Now for the most interesting part. Uncomment the printf statement within the loop. And you will see everything works fine. I got no clue what's happening inside gcc.
I want to know if anyone's gcc(MinGW) compiler is also behaving the same or not.