www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/25/17:32:39

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Converting doubles to ints and chars!?
Date: Tue, 22 Jul 1997 22:29:59 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 42
Message-ID: <33D53467.259F@cs.com>
References: <33d535b4 DOT 193109 AT news DOT algonet DOT se>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp205.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


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