0

I am having a problem converting string to a double. Please help me out.

Here's my code:

char Price[100];
double newPrice;
printf("\nPlease enter the price:");
fgets(Price,100,stdin);
newPrice = atof  (Price);
printf("\nPrice of item is %f",newPrice);

It gives diff output while running with diff files.

file1 (method 1):

char   Price[100];
double newPrice;

int main()
{
    myval();
    return 0;
}

myval()
{
    printf("\nPlease enter the price:");
    fgets(Price, 100, stdin);

    newPrice = atof(Price);
    printf("\nPrice of item is %f", newPrice);  
}

file2 (method 2)

#define MAX 50

char    oldPrice[MAX];
double  newPrice;

int main()
{
    UserInput(); 
}

int UserInput()
{
    printf("\nPlease enter the price:");
    fgets(oldPrice, MAX, stdin);

    newPrice = atof(oldPrice);
    printf("\nPrice of item is %f", newPrice);  

    return 0;
}

The above two methods were compiled using tcc(tiny Compiler). Both methods are the same , but i get a diff output for these 2. output 1:

D:\>new.exe

Please enter the price:12.3

Price of item is 12.300000

Output 2:

D:\>t.exe

Please enter the price:12.3

Price of item is 7735248.000000
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
eminem92
  • 1
  • 1
  • Technically there's no difference between the two functions (except the undefined behavior by not returning something from the `myval` function in the first example). Are you sure you build the second example with the shown code before trying it? – Some programmer dude Mar 24 '14 at 10:10
  • @JoachimPileborg - I don't currently understand it, but [repro on ideone](http://ideone.com/PBG2K1) – Chowlett Mar 24 '14 at 10:13
  • Ah, although [the first one also fails](http://ideone.com/U8JUnd). Although I still don't know _why_. – Chowlett Mar 24 '14 at 10:18
  • 2
    I can reproduce the behaviour if I don't include `` for `atof`. That seems to be the real difference between the two code snippets above, from which the include directives were, er, excluded. – M Oehm Mar 24 '14 at 10:20
  • @MOehm - huh. Yes. So... what's being picked up for `atof` when it's not included, I wonder? – Chowlett Mar 24 '14 at 10:23
  • 1
    @Chowlett: In C89, C silently assumes `int` as return value. In C99, this shouldn't compile without prototype. (When I define a double of value 12.3 and do some pointer nastiness to get an int which then is print-effed with `"%f"`, I can reproduce the original result on my machine.) – M Oehm Mar 24 '14 at 10:28
  • Thank You @MOehm i got the correct answer by including the lib file(stdlib.h). – eminem92 Mar 24 '14 at 11:04

2 Answers2

1

You must include <stdlib.h> to get the right prototype for atof:

double atof(const char *);

With that, the behaviour is as expected. (I reckon that you included it for the first snippet, but not for the second.)

The compiler should warn you about using a function without prototype. Using a function without prototype is legal in C89, but the compiler will assume that the return value is int. That and printig the value with type-unaware printf seems to lead to the strange output.

In C99 and newer standards, it is illegal to use a function without prototype. So I recommend to use at least C99 if your compiler supports it. As a minimum, turn on compiler warnings.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
0

You are missing a include in your file.

#include < stdlib.h >

See also Converting char* to float or double

Community
  • 1
  • 1
MrSykkox
  • 528
  • 4
  • 14