-----BEGIN PGP SIGNED MESSAGE-----

Hash: SHA1

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi there, i'm a newbie in c.

> I'm using Turbo C 2.0 compiler.

> Here is my question:

>

>

> main(){

> int a=1, b=2;

> float c;

>

> c=a/b;

> printf("%f", c); /* why i get "0.0000"? */
Since a and b are int, division is done in the int domain

1 goes into 2 zero times with 1 remainder

so, the result is 0

Since c is a float, the results of the int division are converted to float

0 becomes 0.0

>

> c = (float)(a/b);

> printf("%f", c); /* why i get "0.0000" again? */
Since a and b are int, division is done in the int domain

1 goes into 2 zero times with 1 remainder

so, the result is 0

The result is then cast to float

0 becomes 0.0

Since c is a float and the results of the computation are in float, no

conversion is performed

0.0 remains 0.0

> c = (float) a/b;

> printf("%f", c); /* then i get "0.5000" this time */
a and b are int

(float) a converts a into a float, so 1 becomes 1.0

Now the division is between a float and an int, and so the computation and the

results are float

1.0 / 2 becomes 1.0 / 2.0 which becomes 0.5

Since c is a float and the results of the computation are in float, no

conversion is performed

0.5 remains 0.5

> }

>

>

> so, 1/2=?

>

> thanks!

>
- --

Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request

Registered Linux User #112576 (

http://counter.li.org/)

Slackware - Because I know what I'm doing.

-----BEGIN PGP SIGNATURE-----

Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCu22eagVFX4UWr64RAuE9AJ9VegjhFu3qzvZexaRRts cNmPNiQwCg0r37

wm1fKvBgiVrhhrkSo+PyKOw=

=85zE

-----END PGP SIGNATURE-----