Topic: usual arithmetic conversions


Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/06/23
Raw View
I have two questions concerning usual arithmetic conversions.

1.) all 'char' types (signed, unsigned and undecorated) are
converted to an int before the evaluation of the expression,
so do they then return an int? For example:

   char a;
   char b;

   a + b

would this then return an int which is then (possibly) converted
into whatever the result is assigned to?

2.) What happens with unsigned short int and signed short ints?  If they

are used in an expression, say:

   short int x;
   short int y;

   x + y

are these both promoted to int and return an int, or are they left as
short int and return short int.  The same question would apply to a
short int added to a char.  Based upon the rules as I read them they
would,
but I'm not totally positive about this.

Thanks,
Terence Kelling
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/06/23
Raw View
Terence Kelling wrote:
>
> I have two questions concerning usual arithmetic conversions.
>
> 1.) all 'char' types (signed, unsigned and undecorated) are
> converted to an int before the evaluation of the expression,
> so do they then return an int? For example:
>
>    char a;
>    char b;
>
>    a + b
>
> would this then return an int which is then (possibly) converted
> into whatever the result is assigned to?

Yup.

> 2.) What happens with unsigned short int and signed short ints?  If
> they are used in an expression, say:
>
>    short int x;
>    short int y;
>
>    x + y
>
> are these both promoted to int and return an int, or are they left as
> short int and return short int.  The same question would apply to a
> short int added to a char.  Based upon the rules as I read them they
> would,
> but I'm not totally positive about this.

Everything is promoted at least to an int within the expression. If the
result is assigned to something, or used to initialize something
(including perhaps a function parameter), it is truncated if the type is
smaller. Some compilers warn of the potential loss of information when
this happens, although it is pretty common programming practice to do
this. In fact, a=b+c has the same problem, since adding two n-bit
numbers really produces an n+1 bit result, but compilers don't bother to
warn about that.

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]