Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #139035
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: A final word on casting. |
| Date | 2018-12-24 14:52 -0800 |
| Organization | None to speak of |
| Message-ID | <ln7efyczbk.fsf@kst-u.example.com> (permalink) |
| References | <af569436-b2e1-492b-8379-9f927013c590@googlegroups.com> <41c52811-03f6-4546-a2d4-fe85d9d27411@googlegroups.com> |
chanrasjid@gmail.com writes:
> I think I have got the final word on casting:
> "It is the set of rules to convert from one data type to another."
> I think it is what Ben and others have said. It is as simple as that -
> hope I am right this time.
No, that's not what "casting" means.
The term "cast" is often *incorrectly* used to refer to type conversion
in general.
"Conversion" means taking a value of some type and obtaining from it a
value of a (probably different) type. A conversion can be an implicit
conversion (for example, in a simple assignment) or an explicit
conversion. An explicit conversion is specified by a *cast*, an
expression consisting of a type name in parentheses followed by an
expression. (The parenthesized type name is a cast operator).
The word "cast" refers *only* to that particular kind of expression.
A cast specifies exactly the same operation that would have been
performed if the same conversion had been done implicitly (*if* it's a
conversion that can be done implicitly; most pointer conversions require
casts).
(Strictly speaking, a unary-expression with no cast operator is also a
cast-expression, but that's only for syntactic convenience, consistent
with the way other kinds of expressions are defined in the grammar.)
> Just to satisfy myself, I google for "C casting" and read what some
> articles teach about C casting. Basically, it is what I seem to
> already know.
I suspect a lot of those articles repeat the same misconception, that an
implicit conversion is a "cast". It isn't.
> The only thing that troubled me in the past was the casting of signed
> int to unsigned :
> while (number < 0) {
> number += MAX_UNSIGNED_INT + 1
> }
That's a conversion, not a cast (and the macro is UINT_MAX).
> It was explained that the cast here does not change the bit patterns;
> both values have the same bits, but values will be different as they
> depend on interpretation of the bits. I was thinking along why would
> anyone entertain having one of its computed -ve value assigned to an
> unsigned int. No one starts off with : unsigned int x = -123, y =
> -23,... rarely. So what is the purpose of casting.
A conversion from a signed integer type to an unsigned integer type of
the same size *typically* doesn't change the bit pattern, but that's not
how it's defined (though the way it's defined is certainly motivated by
that). On a system that uses ones'-complement or sign-and-magnitude,
rather than the vastly more common two's-complement, the conversion of a
negative integer to an unsigned type does change the bit pattern.
> As C allows for assignment of one type to a variable of a different
> type, a set of casting rules is needed.
> So this is C type casting.
No, this is C conversion.
Converting any integer to an unsigned type is well defined -- mostly
because the result can be specified in a way that's easy to implement.
The standard's description of "repeatedly adding or subtracting one more
than the maximum value that can be represented in the new type until the
value is in the range of the new type" is not how it's actually
implemented; it's a way of expression, while referring only to *values*,
something that's actuallly done by simple a manipulation of the
*representation*. The bits of the source value are simply copied, or
the high-order bits are discarded, or zero bits are added at the top,
depending on the relative sizes (*if* the system uses two's-complement).
Converting any integer to a signed type is defined more loosely. If the
converted value is outside the range of the target type, the result is
implementation-defined.
--
Keith Thompson (The_Other_Keith) kst@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A final word on casting. chanrasjid@gmail.com - 2018-12-23 03:02 -0800
Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 12:11 +0000
Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 13:08 +0000
Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 14:43 +0000
Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 14:56 +0000
Re: A final word on casting. chanrasjid@gmail.com - 2018-12-23 07:08 -0800
Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 15:55 +0000
Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 16:07 +0000
Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 08:21 -0800
Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 17:56 +0000
Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 19:23 +0000
Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 20:12 +0000
Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 14:06 -0800
Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 09:34 -0800
Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-28 20:29 +0000
Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 15:07 -0800
Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-28 20:56 -0500
Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-29 09:22 -0800
Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2019-01-01 13:27 -0800
Re: A final word on casting. Bart <bc@freeuk.com> - 2019-01-02 01:07 +0000
Re: A final word on casting. Bart <bc@freeuk.com> - 2019-01-03 18:55 +0000
Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2019-01-03 22:27 -0500
Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-04 16:25 -0500
Re: A final word on casting. Ike Naar <ike@iceland.freeshell.org> - 2018-12-28 19:10 +0000
Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-28 12:33 -0800
Re: A final word on casting. "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-23 14:11 -0800
Re: A final word on casting. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-12-24 02:54 -0800
Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-24 03:24 -0800
Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-24 03:29 -0800
Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-24 03:45 -0800
Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-24 13:44 +0000
Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-24 08:51 -0500
Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-23 14:53 -0500
Re: A final word on casting. Robert Wessel <robertwessel2@yahoo.com> - 2018-12-24 09:39 -0600
Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-24 13:38 -0500
Re: A final word on casting. Manfred <noname@add.invalid> - 2018-12-23 21:22 +0100
Re: A final word on casting. Manfred <noname@add.invalid> - 2018-12-23 21:33 +0100
Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 10:56 -0500
Re: A final word on casting. Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2018-12-23 17:18 +0100
Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 20:03 +0000
Re: A final word on casting. "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-28 14:47 -0500
Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 12:24 +0000
Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-23 07:28 -0500
Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 07:50 -0800
Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-23 04:35 -0800
Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-23 04:42 -0800
Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-23 04:52 -0800
Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 10:28 -0500
Re: A final word on casting. Barry Schwarz <schwarzb@dqel.com> - 2018-12-23 11:39 -0800
Re: A final word on casting. Ike Naar <ike@iceland.freeshell.org> - 2018-12-24 07:56 +0000
Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 14:03 -0800
Re: A final word on casting. Barry Schwarz <schwarzb@dqel.com> - 2018-12-24 14:39 -0800
Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 14:50 -0800
Re: A final word on casting. Keith Thompson <kst-u@mib.org> - 2018-12-24 15:34 -0800
Re: A final word on casting. Keith Thompson <kst-u@mib.org> - 2018-12-24 15:23 -0800
Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 20:34 -0800
Re: A final word on casting. Keith Thompson <kst-u@mib.org> - 2018-12-24 14:52 -0800
Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 20:25 -0800
Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-24 17:52 -0500
Re: A final word on casting. luser droog <luser.droog@gmail.com> - 2018-12-24 21:27 -0800
Re: A final word on casting. luser droog <luser.droog@gmail.com> - 2018-12-24 21:39 -0800
Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 22:36 -0800
csiph-web