Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

atof problem..

Status
Not open for further replies.

ekobudy

Programmer
Jul 28, 2002
42
0
0
ID
Hello guys,
i'm new in C..

i have code like this :

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

char *inputstr;
double atofret;

void main() {
inputstr="1234567890.1234";
atofret=atof(inputstr);
printf(" Converted to : %14.4f \n",atofret);
}

the result when i run the program is :
Converted to : 12345678901234.1230


The question is ?
why the string input and the atof output aren't same 1234567890.1234 ?

and if there any way to make it same input and output.. can somebody tell me ?

Thank you.

regards,

Ekow
 
> void main() {
main returns an int, as in
Code:
int main ( ) {
  // your code
  return 0;
}

> why the string input and the atof output aren't same
floats and doubles are approximations. The best you'll ever get out of a double variable is about 15 decimal digits of precision.
Your example fails at the 14th decimal digit.

> and if there any way to make it same input and output.. can somebody tell me ?
Only by using high precision maths libraries like say is this likely.

--
 
Some C compilers have the concept of a long double. Maybe you could try that.
 
Using MinGW ( gcc 3.3.1) I'm getting long double being 12 bytes, and double as being 8.
 
Code:
 inputstr="1234567890.1234";
    atofret=atof(inputstr);
    printf(" Converted to : %14.4f \n",atofret);

hi .. although you will get away with this in most compilers, don't do this without allocating memory first. you will seg fault.

also, atoi, atof etc. explicity look for a NULL at the end. so, a better way to do this will be to add a NULL [after finding string length] and then invoking atof.

br:
 
String literals are automatically null-terminated (by the compiler) as far as I know:

A string in C is simply an array of characters, with the final character set to the NUL character (ascii/unicode point 0). This null-terminator is required; a string is ill-formed if it isn't there. The string literal token in C/C++ ("string") guarantees this.
const char *str = "foo";

is the same as
const char *str = {'f', 'o', 'o', 0};
 
xwb, can you tell me about long double and where can i find these compiler?
 
sethmcdoogle <= it is valid only at variable declaration, only at application level [user mode] however.

in this case, declaration and assignment are different instructions. besides, as mentioned in my previous post, we will get away with it in most compilers .. not always the case.

br:
 
Which OS are you using?

As sethmcdoogle mentioned, it is available on gcc (Unix & Windows).

On Windows, it is available on Visual C++ as well.
 
Ok, thank you xwb.
I'm working on Solaris9.

thanks, for the information.
I will download gcc first.

Thank you to all of you guys.

regads,

ekow
 
jmnagi:
> don't do this without allocating memory first. you will seg fault.

everything seem to be allocated?
 
jmnagi is wrong.
Code:
char *inputstr="1234567890.1234";
Is both always allocated, and always nul-terminated.

Where you can come unstuck is when you try and modify a string constant. Given the above assignment
Code:
inputstr[0] = '2';
Most modern combinations of OS and compiler will generate code which will trap when you attempt this assignment.

"string" constants can be placed in read-only memory. If the compiler chooses to, and the OS enforces it, attempts to modify read-only memory will generate an exception.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top