Mail Archives: djgpp/1996/07/22/09:57:52
At 22:43 21/07/96 GMT, Jan Louwerens wrote:
>David L Clayton (dclayton AT dircon DOT co DOT uk) wrote:
>: Could somebody please tell me the best way to change an integer number to a
>: float, and vice versa?
>
>just type cast it.
>
>int i = 4;
>float f = 6.0;
I do not want to play finicky here but for the sake of completeness, it
should be emphasized that a constant "6.0" in ISO (ANSI) C has type double
======
sometimes we find this kind of example even in books of good origin (K&R 2nd
Ed. inclusive).
But the correct would be to use 6.0f or 6.0F to really keep clear to the
compiler that what you intend is a float constant at all.
>
>float to int:
>i = (int)f;
>
>int to float:
>f = (float)i;
This last cast although it is healthier to keep for the sake of documenting
the code, it is redundant due the way the type promotions occur in C. You
can find a full discussion of it in you favorite C programming reference.
>
>JL
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cesar Scarpini Rabak E-mail: csrabak AT ipt DOT br
DME/ASC Phone: 55-11-268-35221Ext.350
IPT - Instituto de Pesquisas Tecnologicas Fax: 55-11-268-5996
Av. Prof. Almeida Prado, 532. Sao Paulo - SP 05508-901 BRAZIL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Raw text -