Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #138946

Re: A final word on casting.

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: A final word on casting.
Date 2018-12-23 12:24 +0000
Organization A noiseless patient Spider
Message-ID <871s68jus5.fsf@bsb.me.uk> (permalink)
References <af569436-b2e1-492b-8379-9f927013c590@googlegroups.com>

Show all headers | View raw


chanrasjid@gmail.com writes:

> I have been playing with C for about 30 years, but I could never get
> to really understand casting. I made various weak attempts and gave
> up; I knew I don't need to be professional in C.
>
> I take my layman approach to casting: it is to represent the value of
> a certain type in a different data type.

Let me clarify a few terms.

C support converting between values of different types.  These
conversions can be done implicitly or explicitly using a cast operator.
The effect is the same in either case, so what you call casting may be
happening even when there is no explicit cast operator.

So conversions apply to values, whereas objects, in C, are regions of
storage that use bits to represent values.  Casts and implicit
conversions have nothing to do with representations.  The representation
is set when a value is assigned to an object, and it is interpreted to
yield a value when an object is accessed.

> Some things I tested work and I don't care about its correctness. I
> always get it correct: (int) 1.23 always give 1.

Yes, (int)1.23 is always the value 1 of type int.  You get the same
conversion in other contexts too:

  void f(int x);
  ....
  f(1.23);      // 1.23 is converted to int in the call
  int i = 1.23; // 1,23 is converted to int here too.
  
> But if we reason
> things out, if casting tries to represent a float as any integer type,
> it is not possible.

(Note: float is a specific C type, but 1.23 is a constant of type
double.)

The conversion of 1.23 to int does not involve the representation of
either value in any way.  It's an operation that converts a value of
type double to a value of type int.

> So C may here just return the integral part; but
> it seems this is not "official".

No, it's official.  The C language specification says what should
happen.

> Casting a signed int to a long unsigned integer is meaningless if the
> original integer is negative;

That conversion, too, is both well-defined and "official".

> I think the experts know what result would be returned as per some C
> standards;

They do!  The The C standard says what (unsigned long)-1 means: it's the
largest value of type unsigned int.

> but then what is the purpose when it is meaningless.

How can something that has a well-defined meaning be meaningless?  You
mean, presumably, that you don't see the purpose of that conversion.
That's fine.  There are lots of things in this world I don't see the
purpose of.

> The only use I think is safe is casting compatible types to another
> with the larger storage, as int to _int64, or float to double. Casting
> double to float is OK as we expect a loss of precision.

Type conversion (what you call casting) has many more applications than
simply converting between arithmetic types.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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