Reputation: 23
So my code is relatively simple, I am just attempting to get a double from a file (input.dat). I am getting the double, however when I return in to main, I get a different value for some reason. Here's the code:
int main(void) {
FILE *infile;
infile = fopen("input.dat", "r");
double data = read_double(infile);
printf("%lf", data);
return 0;
}
double read_double(FILE *infile) {
double data = 0;
//infile = fopen("input.dat", "r");
fscanf(infile, "%lf", &data);
printf("%lf\n", data);
return data;
}
What's actually in input.dat
11234567.0
So when I run the program, the print statement in read_double is printing the correct number. But when I return that to main, and print it in main, it's printing to 16.000.
When I get rid of the print statement that's in read_double, then main prints 1.000. I don't really know what to do right now, I'm wondering if this has something to do with the way that data is stored and transferred? Any help is appreciated, thank you.
Upvotes: 2
Views: 1348
Reputation: 14177
You need to declare the return type of your read_double() function in the source file that defines main(), or in an included header. Otherwise, C doesn't "know" it returns a floating point double type.
double read_double(FILE *infile);
int main(void) { ....
Upvotes: 4