This morning in my CS lab, I was compiling and executing some C code. The code are as follow:
#include <stdio.h>
#define SIZE 3
int main()
{
float x[SIZE];
float *fp;
int i;
for(i=0; i<SIZE; i++)
//{
x[i] = 0.5*(float)i;
//}
for(i-0; i<SIZE; i++)
//{
printf(" %d %f \n", i, x[i]);
//}
fp=x;
for(i=0; i<SIZE; i++)
//{
printf(" %d %f \n", i, *(fp+i));
//}
}
This is the command I used in Ubuntu terminal to compile:
gcc -o program program.c
This is the command i used to execute
./program
This is the result I encountered:
0 0.50000
1 1.50000
2 2.50000
This the result I expected and found on other computers in the same lab.
0 0.50000
1 1.50000
2 2.50000
0 0.50000
1 1.50000
2 2.50000
Disclaimer: I double checked and triple checked the my code is entirely intact with ones in the other computers.
Clearly, something is wrong with the compiler here. Can anyone advise me what is the problem? And how to fix it?
Regards
Your problem is:
This makes the for-statement have no effect and hence the
printfthat follows gives no outputIf you compile it with strict warnings the compiler shall tell you so.
Output:
Once you fix the obvious typo, all compilers will generate the output twice.