Normally I prefer using integers so I never really noticed the problem earlier, but recently I was just playing around with some float values and found out something is really strange here...
I opened a console project and created two source code files: "main.c" and "func.c"
I wrote the following on each:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* main.c */
#include <stdio.h>
#include <stdlib.h>
int main(){
double n;
n = getnum();
printf("\n\n\t\t %lf \n\n", n);
system("pause");
}
In order to compile main.c (assuming you're compiling it as C++ - this is a C++ forum so that seems reasonable, although it's a choice of file extension that's just asking for trouble with your compiler), the compiler has to know the input parameters and return parameter of your function getnum. This is the sort of thing usually done with a header file; it doesn't have to be done with a header file, but it does have to be done.
As I recall, C89 allowed it. Given that your file extensions mark it as C code, are you definitely compiling it as C++? If you're just letting the compiler do what it likes, it could well be compiling this as C.
Well... at the moment I am more like learning pure C just to get some experience with the language. Since I can compile a C program with a C++ compiler I never really cared if I was compiling it as C++ or C...
Back to the function. Yesterday I made some tries to make the function work. I changed the "atof()" function for a "scanf("%lf", d)" but no matter what I did the function always returned a strange value.
Well... at the moment I am more like learning pure C just to get some experience with the language. Since I can compile a C program with a C++ compiler I never really cared if I was compiling it as C++ or C...
You can often compile a C program with a C++ compiler. The code as above will not compile under a C++ compiler (and won't compile under some C compilers, depending on the version of C they conform to). C++ demands to know the prototype of a function before you use it.
I have no Idea why, but the function is doing what I want, but is returning a fake value.
I'll see if I can get myself a newer version then...
My code's working so I'm good for now...
You say I should have a header file containing all my prototypes. Wouldn't it be enough to have the prototypes in the same file the function is declared (or in the file the function is used).
I mean, I never used a header with prototypes and my programs always worked perfectly. This function was the first to show a problem.
You say I should have a header file containing all my prototypes.
No I didn't. I said this:
This is the sort of thing usually done with a header file; it doesn't have to be done with a header file, but it does have to be done.
I mean, I never used a header with prototypes and my programs always worked perfectly.
Really? So what are these?
1 2
#include <stdio.h>
#include <stdlib.h>
They're headers, with prototypes (and a whole bunch more).
Think bigger. Imagine you've written some functions or classes that you will need to use in more than one cpp file. Your options are to either copy them out into each cpp file over and over and over and over, and each time you make a change to go back and make the change over and over and over and over, or just put them in a header file once.