Mail Archives: djgpp/1997/02/17/22:42:28
nikki wrote:
>
> i eventually figured this one out, it can be solved by ensuring a #include
> <stdlib.h> presumably linux and sunos were doing this automatically or
> something. but it struck me as weird behavior nonetheless, after all it
> compiled fine (ok -Wall would say implicit declaration, but you can do that
> with printf in the example above and printf has the correct behavior?)
Ahhh... this is a programming mistake on your part. You MUST ALWAYS
include the appropriate header files so that the compiler knows what
data type a function is supposed to return! In the case above, atof()
is being implicitly declared to return an integer, not a float, which
gets passed to printf() as a 4 byte entity. Since printf() expects to
see 8 bytes for the '%f' specifier, it only grabs half of the value
returned from atof() and goes apeshit. :) In practice, it can become
even more complex than that, because atof() really _does_ return a
float...
Simple answer: always, always, include the right header files! If you
had compiled with '-Wall', you'd have been warned about implicit
declaration of 'atof', and also about mismatched parameters to
printf(). '-Wall' is the programmer's life preserver. Omit it at your
own risk.
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | fighteer AT cs DOT com |
| * Proud user of DJGPP! * | http://www.cs.com/fighteer |
| ObJoke: If Bill Gates were a robber, not only would he |
| shoot you, but he'd send you a bill for the bullets. |
---------------------------------------------------------------------
- Raw text -