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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using atof

Status
Not open for further replies.

mstewart

Programmer
Jul 5, 2001
9
US
I've been having a problem with a program that I can't seem to figure out why it isn't working, so I thought I would ask about it here.

This is a program written in C and is using the newest HP ANSI C compiler. Previously, this code was compiled using an older compiler and did not have any issues.

Basically, within the program, I am reading a value from a column into a string. I am then using atof() to store this string as a double. I'd rather it'd just be a float, but that's a separate issue I guess. The problem is, I checked it using a simple printf statement directly afterward and the double value appears to be a memory address(ie. - a very large integer) instead of the 3 decimal place double that it should be. I also tried converting straight from the read, skipping the interim string variable and got the same result.

Am I doing something wrong with this? Should I be accessing something as a pointer and I'm not?
 
Paste some sample code which demonstrates the problem.



--
 
Check up your printf() format specification. Use %f, %e or (better) %g (may be it's %d now;)
 
I'm pretty sure it's not the printf function, since I do an assert with the same value which also fails. I'll try posting up the code shortly.
 
Turns out it was missing the stdlib.h include file.

Still don't understand how it compiled on the older version of the compiler without it, but owell.

Thanks for the help everyone!
 
> Still don't understand how it compiled on the older version of the compiler without it
In really old C, the compiler makes lots of assumptions about things. At the end of the day, so long as all the symbols were spelled correctly (so the linker wouldn't complain), it really didn't matter too much whether you missed the occasional header file.

The problem fell to the programmer to ensure that you did
result = atof( "123.456" );
and not
result = atof( 123.456 );
The compiler was happy, the linker was happy, and you only found out when the program blew up.

Nowadays, ANSI-C pretty much states that you have to have prototypes for everything, which is a big improvement when it comes to getting code right.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top