Deutsch   English   Français   Italiano  
<20240809142622.467@kylheku.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Kaz Kylheku <643-408-1753@kylheku.com>
Newsgroups: comp.lang.c
Subject: Re: how cast works?
Date: Fri, 9 Aug 2024 21:30:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <20240809142622.467@kylheku.com>
References: <v8vlo9$2oc1v$1@dont-email.me> <slrnvb7kis.28a.dan@djph.net>
 <v929ah$3u7l7$1@dont-email.me> <87ttfu94yv.fsf@nosuchdomain.example.com>
 <v93a3t$6q7v$1@dont-email.me> <v93e2q$8put$1@dont-email.me>
 <87bk228uzg.fsf@nosuchdomain.example.com> <v94pji$m1ib$1@dont-email.me>
 <v95lb7$26koh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 09 Aug 2024 23:30:33 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="55a0cda964873bc28d399539d9ad18f7";
	logging-data="210922"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+wNgEH0uqA3JsJJDEvclRTyctJ7TnX1Do="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:hkjoeLL0rRK8DSN3EPhb0KwoXbg=
Bytes: 3096

On 2024-08-09, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
> On 09/08/2024 12:04, Bart wrote:
>> On 09/08/2024 00:17, Keith Thompson wrote:
> ...
>>> There is no such thing as an "implicit cast" in C.
>>>
>> 
>> Suppose I write this code:
>> 
>>      x = a;                  // implicit 'conversion'
>>      x = (double)a;          // explicit 'conversion'
>> 
>> 
>> My compiler produces these two bits of AST for the RHS of both expressions:
>> 
>> 1 00009 r64---|---2 convert: sfloat_c i32 => r64
>> 1 00009 i32---|---|---1 name: t.main.a.1
>> 
>> 1 00010 r64---|---2 convert: sfloat_c i32 => r64
>> 1 00010 i32---|---|---1 name: t.main.a.1
>
> Of course - an implicit conversion has exactly the same effect as a
> explicit conversion, if the source and destination types are the same.
> That doesn't make it correct to use the term "cast" to describe anything
> other than an explicit conversion.

That's all very neat and clean. However, the problem is that in C,
some of the implicit conversions are unsafe.

Implicit conversions can:

- truncate an integer value to fit a narrower type.

- convert between floating point and integer in a way that the value
  is out of range, with undefined behavior ensuing.

- change the value: e.g -1 becomes UINT_MAX.

- subvert the type system, e.g.  (foo *) -> (void *) -> (bar *).

In computer science, we refer to unsafe conversion as coercion.
THe cast notation is C's coercion operation.

In some languages, some of what C allows to be an implicit conversion
would be classified as requiring a coercion.

It almost makes sense to speak of "implicit cast" (i.e. coercion) in C,
because of what happens implicitly being so unsafe.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca