🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #49783

Re: strange output

#49783
From: Richard Earnshaw
Date: Thu, 09 Dec 1993 08:58
47 lines
2623 bytes
In article <9312071842.AA02949@aaron.music.qc.edu>, richards@aaron.music.qc.edu (David Richards) writes:
|> Hello folks - here's the problem:
|> 
|> #include <stdio.h>
|> main()
|> {
|>    long double ldb = 22343546554.435551;
|>    printf("ldb = %f\n", ldb);
|> }
|> 
|> compile with: gcc -o longd longd.c
|> BUT THE OUTPUT READS: ldb = 8.500005
|> 
|> Forgive me if I'm dreaming but it looks like a bug to me!!
|> Am I missing something that's so painfully obvious that an
|> amoeba could see it?!#%@^??
|> I'm running on NeXT 3.1- when using cc this doesn't  
|> happen.
|> Dave Richards
|> 

When passing a long double to printf the format string should be of the form
"%Lf".  Use -Wformat when compiling with gcc to get warnings of problems of
this nature.  Note, however, that if your printf is supplied by the library
for the native compiler, that it might not support such a mode.

Run the following program through gcc and the native C compiler, if the
answers differ then the following explanation is the one you are looking for.

#include <stdio.h>

main ()
{
    printf ("long double is %d bytes\n", sizeof (long double));
    printf ("double is %d bytes\n", sizeof (double));
}

The chances are that the native cc supports long double as the same sized
object as a double (in the same way that long and int are often the same size
on many 32-bit machines).  If that is the case, then to the native c compiler
your program is no different than if you had declared ldb as a double.

Gcc on the other hand, can support long doubles as a larger type, provided
that the hardware of the machine will do so (I don't have a NeXT machine so I
don't know).  However, the standard library that comes with the system doesn't
know how to deal with these (or was incorrectly told how to do so), so printf
gets confused when unpicking the arguments from the stack.  
Technical Details
Message-ID: <CHrFMB.1HK@cs.utwente.nl>
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!zib-berlin.de!zrz.TU-Berlin.DE!netmbx.de!Germany.EU.net!EU.net!sun4nl!utciva.civ.utwente.nl!infnews.cs.utwente.nl!rwe
References: <9312071842.AA02949@aaron.music.qc.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,6f7a036dfb120868
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1993-12-10 05:23:47 PST
Newsgroups: gnu.gcc.help
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!zib-berlin.de!zrz.TU-Berlin.DE!netmbx.de!Germany.EU.net!EU.net!sun4nl!utciva.civ.utwente.nl!infnews.cs.utwente.nl!rwe
From: rwe@cs.utwente.nl (Richard Earnshaw)
Subject: Re: strange output
Message-ID: <CHrFMB.1HK@cs.utwente.nl>
Sender: usenet@cs.utwente.nl
Nntp-Posting-Host: hangjas.pegasus.esprit.ec.org
Organization: Twente University, Dept. of Computer Science
References:  <9312071842.AA02949@aaron.music.qc.edu>
Distribution: gnu
Date: Thu, 9 Dec 1993 08:58:59 GMT
Lines: 47