Thread: atof returns weird values

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    atof returns weird values

    HI I have recently started learning to use C and have been having a weird experience using atof, it never returns the correct value for me.

    I created a small program to test this and the values it returns are really strange. I have intentionally created the functions in different files because the real program I am working which requires use of atof is set up this way.

    For example if I enter 1 into the program it will return the value entered as 0, 12 will give me a value of 524288.00 ????
    I thought it might have something to do with the data type so I switched from double to float to no avail.

    Code:
    #include <stdio.h>   
    float read_input(){ 
    	char sinput[256]; 	
            gets(sinput);	 		 	
            float dinput; 	
            dinput = atof( sinput );  
     	return dinput; }
    Code:
    #include "read_input.c" 
    #include <stdio.h>
     
         main(){ 	
            float lower;	 	
            printf("please enter a value"); 	
            lower=read_input(); 	
            printf("you have entered %f\n",lower); }
    Any help in understanding this would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    #include "read_input.c"
    You are literally including whatever in read_input.c into your main source file.
    That's not separate compilation.

    What you should do is:

    Code:
    in read_input.h
    
    float read_input(void);    // function prototype 
    
    in your main source file
    #include "read_input.h"
    
    in your read_input.c        // name need not be same. .c .h etc
    #include "read_input.h"
    float read_input(void) {
    ...
    }
    Don't use gets(). Read the faq why.
    atof() prototype is in stdlib.h you need to include it.
    If you don't include respective header file which has prototype, compiler will assume function as returning int and taking unspecified argument.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yes - you need to include stdlib.h -- that will resolve the behaviour you're seeing.

    Quote Originally Posted by Bayint Naung
    You are literally including whatever in read_input.c into your main source file.
    That's not separate compilation.
    Indeed -- what you have done is exactly the same has putting everything in one file (the preprocessor, which handles include directives, will basically concatenate the files before compilation).

    The way suggested (use a header file) is the 'right' way. Alternatively you could just put the function prototype in your main.c

    Code:
    float read_input(void);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  2. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM