problem with precision when reading from file

Anna Sidera sidera.anna@ucy.ac.cy
Wed Sep 15 14:40:00 GMT 2010


Hello, 

I wrote the following program:



#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h> 

int main() 
{ 

FILE *fp; 
fp = fopen("test.c", "r"); 
if (fp == NULL){ 
    printf("Error opening input file\n"); 
    exit(1); 
} 

float var1, var2, var3; 

fscanf(fp,"%f",&var1); 
fscanf(fp,"%f",&var2); 
fscanf(fp,"%f",&var3); 

if (var1!=0.01) printf("var1 = %g\n",var1); 
if (var2!=5) printf("var2 = %g\n",var2); 
if (var3!=0.6) printf("var3 = %g\n",var3);



Where test.c contains the following: 

0.01 
5 
0.6



When I run the program I get the following output: 

var1 = 0.01 
var3 = 0.6 



I expected to get no output. Can you explain what is the problem? 

Thanks, 
Anna



More information about the Gcc-help mailing list