www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/31/10:43:00

Date: Sun, 31 May 1998 10:42:53 -0400 (EDT)
Message-Id: <199805311442.KAA17864@delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: seawolf AT net4you DOT co DOT at
CC: djgpp AT delorie DOT com
In-reply-to: <35715A2E.A9A35953@net4you.co.at> (message from Seawolf on Sun,
31 May 1998 15:25:02 +0200)
Subject: Re: A nice trap!

> void main( void)
> {
>     float f;
>     f = 55 / 77;
>     printf( "%f", f);
> }
> /* and please mail me YOUR results */

It does exactly what I expect: it prints 0.  If you expect anything
else, you don't understand how C works.  When you do this:

	f = 55 / 77;

The compiler evaluates the 55/77 part first - the division of two
*integers*.  Integer division returns an integer result (0).  *Then*
it's converted to a floating point number (0.0) in order to store it.

If you want the compiler to divide floating point numbers, you have to
tell it to do that:

	f = 55.0 / 77.0;

- Raw text -


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