Re: [cc65] Type conversions

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2002-12-20 00:33:25


Hi!

On Thu, Dec 19, 2002 at 11:01:31AM -0800, Shawn Jefferson wrote:
> How do type conversions work in cc65?  What are the rules that the compiler
> follows when evaluating lines such as:
[...]

You examples would be evaluated using long arithmetic. cc65 follows the ISO
standard. If you find a place where this is not true, it's a problem with the
compiler.

As a general rule: Before doing any arithmetics, characters are always
converted to ints. If one operand of an expression is a long, the other is
also converted to long. If both operands are ints, and the result does not fit
into an int, it will overflow. So the following does not work:

        int a = 10000, b = 10000;
        int c = a * b;

To make it work, you will have to cast one of the operands (or both) to long,
so that the expression is evaluated with long arithmetic.

> I've found that I've had to put (long) in a few places to get things working
> correctly with 2.9.0.  Is it good practice to always do this, or let the
> compiler decide for you?
 
It's good practice to do it in the places where it is needed:-) Seriously: I
think the best way would be to understand which rules are applied
automatically applied by the compiler.

> With gcc, type conversions always seemed to be
> pretty transparent (to me.)

gcc on x86 has 32 bit ints and 32 bit longs. This means that you can mix the
two freely without any different code being generated. The example above
(which doesn't work with cc65) would work using gcc. However, it is still non
portable, because the standard guarantees only 16 bits for ints, and there are
a lot of other compilers out there, that have 16 bit ints. This is especially
true if the target platform is small.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2002-12-20 00:33:31 CET