delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1997/06/14/22:47:40

From: colin AT bird DOT fu DOT is DOT saga-u DOT ac DOT jp (Colin Peters)
Subject: RE: Amendment to post: atof() bug in b17.1 & b18?
14 Jun 1997 22:47:40 -0700 :
Approved: cygnus DOT gnu-win32 AT cygnus DOT com
Distribution: cygnus
Message-ID: <01BC798B.63729540.cygnus.gnu-win32@gbird0>
Mime-Version: 1.0
Original-To: "'e DOT dennis AT pemail DOT net'" <e DOT dennis AT pemail DOT net>
Original-Cc: "'GNU-Win32'" <gnu-win32 AT cygnus DOT com>
Original-Sender: owner-gnu-win32 AT cygnus DOT com

E. Dennis[SMTP:e DOT dennis AT pemail DOT net] wrote:
>Hi.  Yes, more info.  Sorry.

Sorry if I sounded abrupt in that last post. However, the results
below just go to show that example code is important.

The code you give compiles and gives correct output on my system
(win95 P100) with either the Cygwin32 or Mingw32 libraries given
that you make one small change. The first line of your code should
read:

#include <stdlib.h>

If you don't do this atof is assumed to return an int, which gets
"converted" to a float, resulting in completely silly numbers. If
I leave out the include I get the same results as you under Cygwin32
and all numbers give the same large (and completely wrong) value
under Mingw32. (Why the results are different from the different
libraries is somewhat baffling I'll admit.)

If you compile with the -Wall option (all warnings on) you get this:

junk.c:3: warning: return-type defaults to `int'
junk.c: In function `main':
junk.c:11: warning: implicit declaration of function `atof'
junk.c:17: warning: implicit declaration of function `printf'

Line 3 is complaining about no return type for main. Line 11 is
the important one, because the implicit declaration assumes an
int to be returned.

After adding an int return type to main and including stdlib.h
and stdio.h all of these warnings go away and the code works
fine.

The moral of the story is to always try compiling with warnings
on. The second moral is to always prototype, especially if you
use functions that don't return int. :)

Hope this helps,
Colin.

-- Colin Peters - colin AT bird DOT fu DOT is DOT saga-u DOT ac DOT jp
-- Saga University Dept. of Information Science
-- http://www.fu.is.saga-u.ac.jp/~colin/index.html
-- http://www.geocities.com/Tokyo/Towers/6162/

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019