Thats not supposed to hapen. Why did it?

Hey ya,

I got a question here about "atof()"

I read it was supposed to return me a double value written in my string.
http://www.cplusplus.com/reference/clibrary/cstdlib/atof/

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");
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* func.c */

#include <stdio.h>
#include <stdlib.h>


double getnum(void){
    double n;
    char s[256];
    
    printf("Enter a number: ");
    gets(s);
    
    n = atof(s);
    
    return n;
}


After compiling and linking I ran the program and the folowing came out:

Enter a number: 10

2293232.000000

Press any key to continue . . .


Beat me if I'm wrong bit those aint the same number...

Can some one tell my why?
Last edited on
Works for me:


Enter a number: 10


10.000000


Although I did have to add code to declare the getnum function in main.c and also got the warning:

warning: the `gets' function is dangerous and should not be used.


What compiler are you using? How does main.c know what getnum is?
I think it's not so much what compiler he's using, but what IDE.
I'm going to guess Dev-C++ :)
I tried both "Dev-C++" [the one I use most] and "Code::Blocks"...

I tried to find out what the default compiler from "Dev-C++" if [probably GCC]
With "Code::Blocks" I used "GNU GCC Compiler"

Both cases had the same error...

Moschops (785):
How does main.c know what getnum is?


I still do not have much experience with projects and linking. Am I supposed to tell the linker something about my function I didn't?


Thanks by now...
Last edited on
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.
Last edited on
Yes, Moschops, right.
You need to declare you function before main.c or use header file.

And try to enable the next option in Code Blocks:
"In C mode, support all ISO C90 programs. In C++ mode, remove ....."

Project Options -> Project's built optons ... -> Compiler Flags.
Last edited on
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.

After a while I changed the "func.c" file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* func.c */

#include <stdio.h>
#include <stdlib.h>


double getnum(void){
    double n;
    char s[256];
    
    printf("Enter a number: ");
    gets(s);
    
    n = atof(s);
    
    printf("\n%lf", n);

    return n;
}


Here what came out:

Enter a number: 10

10.000000

2293232.000000

Press any key to continue . . .


I have no Idea why, but the function is doing what I want, but is returning a fake value.


Anyway, I read this last post and added a "header.h" to the project containing the function's prototype and the problem was solved.

But I still would like to understand why this happened... I mean, I did add the prototype to "func.c" and it still did the same error!



Thanks for guiding me this much...


PS: I don't know if it helps but I changed the file extension to ".cpp" and still got the problem. Only the header was able to solve it.
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.


It does not under gcc V4.4.0
Last edited on
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.



Topic archived. No new replies allowed.