Mail Archives: djgpp/1997/07/25/17:32:39
Christian Granström wrote:
>
> How do I convert an double to an int or an unsigned char???
Basic C/C++ question... consult a book or ask on comp.lang.c or
comp.lang.c++.
> I tried to typecast them like:
>
> double X:
> unsigned char Y;
>
> Y = (unsigned char X);
You put the type in parentheses before the value to be changed. That
should look like:
Y = (unsigned char) X;
BTW, this sort of typecast can have unpredictable effects if X has a
value greater than 255.99999.
> Maybe its not supposed to be done this way at all but the
> compiler does'nt complain about it!!
It may be that the compiler thinks you are declaring X in that
statement, and therefore allocates an automatic variable. Y would then
be assigned the value of that uninitialized variable. It's strange...
and only possible in C++. If you compiled that as C it would have
caused a syntax error.
--
---------------------------------------------------------------------
| John M. Aldrich | "Sin lies only in hurting other |
| aka Fighteer I | people unnecessarily. All other |
| mailto:fighteer AT cs DOT com | 'sins' are invented nonsense." |
| http://www.cs.com/fighteer | - Lazarus Long |
---------------------------------------------------------------------
- Raw text -