Bug 28390

Summary: atof incorrect when stdlib.h not #include'd
Product: Base System Reporter: Tom Garcia <veri2>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.3-STABLE   
Hardware: Any   
OS: Any   

Description Tom Garcia 2001-06-25 01:50:01 UTC
atof returns random odd numbers when stdlib.h not #include'd, otherwise seems to work fine.

Fix: 

#include <stdlib.h>
How-To-Repeat: /* note stdlib.h is not being included */
int main()
{
  printf("%g\n", atof("3.95"));
}
Comment 1 Gregory Bond 2001-06-25 02:10:31 UTC
> atof returns random odd numbers when stdlib.h not #include'd, otherwise seems
>  to work fine.

Calling std library functions (especially those returning anything except 
"int") without the appropriate header include is a bug in your program, and 
has nothing to do with FreeBSD or any other OS.  You would get similar errors 
from a Windows C compiler.

This particular instance is documented in the SYNOPSIS section of the man page
for atof.

This bug would also have been picked up by compiling with the "-Wall" flag.

This PR can be closed.
Comment 2 Peter Pentchev freebsd_committer freebsd_triage 2001-06-25 07:24:50 UTC
State Changed
From-To: open->closed

Many, many functions depend on some header file or other to be included 
in order to function properly.  This has been well-documented in both 
the C standard, and the respective manpages. 

In this particular case, atof(3) fails simply because if there is no 
prototype, the compiler assumes that the function returns int. 
You might explicitly declare atof() to return double, or, much better, 
you should include the appropriate header files.