04-18-2008
C assumes (grrrrr, though a good compiler can warn you) that any function it does not see a prototype for prior to your calling it takes an int and returns an int. Since you did not include stdlib.h, it makes this very incorrect assumption for both atof and strtod. Your explicit cast to double even prevents the warning from popping out (explicit casts can be evil!).
It could be that on your platform, you are truncating/misaligning/generally messing up the return value for atof because C assumes that it is int. Due to this assumption, it is performing an (int) -> (double) conversion rather than the correct (no) conversion. Of course, going into the function it is also performing a (char *) -> (int) conversion, which is unusally (but not impossibly) a source of problems too.
In fact, I just recently had a guy come to me with an odd problem which turned out to be caused by a (void *) -> (int) conversion. He said he was passing a pointer to shared memory into a function and was getting "odd" results from this function. My immediate thought that was that pointers to shared memory on our 64 bit architecture machines likely have more resolution than an int (32 bit) can store (ie the upper half of the pointer value is not just zero'd out). I assumed he must not have had included the header and therefore all pointers passed into the function were being truncated to integer resolution. Hence, the real pointer value was never making it into the function and odd results were occuring. He included the header file, all worked well again, I saved the day, lol.
4 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
first off, i LOVE unix to death, but i really only like the text/console side of it. im not big into the XFree86/Xwindows side of things.. so with that known... im in search of really old *nix's.. like way outdated, floppy OS's from way back in the day. im kinda in search of the pure'r UNIX's.... (0 Replies)
Discussion started by: 01000101
0 Replies
2. Shell Programming and Scripting
Does anyone know how to test if an ethernet interface is alive, or accepting connections? Here is the scenario - I have rsc and sc console interfaces on some Suns. There are some sporadic vulnerability scans that send them out to lunch when they run the scans. I have to login to the host and reset... (0 Replies)
Discussion started by: lm_admin_dh
0 Replies
3. Shell Programming and Scripting
Hello,
So I'm utilizing the bash brace expansion feature to checkout multiple folders from cvs with ease, while excluding certain subfolders within. So I do a command like this:
cvs co trunk/{mod_a,mod_b,mod_c} \!trunk/{mod_a,mod_b,mod_c}/web
to checkout modules trunk/mod_a , trunk/mod_b ,... (1 Reply)
Discussion started by: neked
1 Replies
4. Programming
Hi
I have a small code snippet.
printf("sTaTuS |%s| , |%s| => %f , %f \n",before,after,strtod(before,0),strtod(after,0));
If the variables before and after has values 1000.2234 and 1000.0234 then i get the following output.
sTaTuS |1000.2234| , |1000.0234| => 0.000000 , -0.000000
... (1 Reply)
Discussion started by: kumaran_5555
1 Replies
ATOF(3) BSD Library Functions Manual ATOF(3)
NAME
atof, atof_l -- convert ASCII string to double
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <stdlib.h>
double
atof(const char *str);
#include <xlocale.h>
double
atof_l(const char *str, locale_t loc);
DESCRIPTION
The atof() function converts the initial portion of the string pointed to by str to double representation.
It is equivalent to:
strtod(str, (char **)NULL);
The decimal point character is defined in the program's locale (category LC_NUMERIC).
While the atof() function uses the current locale, the atof_l() function may be passed a locale directly. See xlocale(3) for more informa-
tion.
IMPLEMENTATION NOTES
The atof() and atof_l() functions are thread-safe and async-cancel-safe.
The atof() and atof_l() functions have been deprecated by strtod() and strtod_l() and should not be used in new code.
ERRORS
The function atof() need not affect the value of errno on an error.
SEE ALSO
atoi(3), atol(3), strtod(3), strtol(3), strtoul(3), xlocale(3)
STANDARDS
The atof() function conforms to ISO/IEC 9945-1:1990 (``POSIX.1''), ISO/IEC 9899:1990 (``ISO C90''), and ISO/IEC 9899:1999 (``ISO C99'').
BSD
June 4, 1993 BSD