Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: how cast works? Date: Sun, 11 Aug 2024 20:23:20 -0700 Organization: None to speak of Lines: 50 Message-ID: <87le12e85z.fsf@nosuchdomain.example.com> References: <87ttfu94yv.fsf@nosuchdomain.example.com> <87bk228uzg.fsf@nosuchdomain.example.com> <86ttfqin3x.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 12 Aug 2024 05:23:20 +0200 (CEST) Injection-Info: dont-email.me; posting-host="95de69de4fa23f7b87735178c0e02b79"; logging-data="3277109"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mQVO6lHjbxoycVZawTtdB" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:CrSYbDx4+6SscZlCBY0se8ULp7A= sha1:XbGSH6VrXJzukQVcRikUIswob9M= Xref: csiph.com comp.lang.c:387496 Bart writes: > On 12/08/2024 01:46, Tim Rentsch wrote: >> Keith Thompson writes: >> >>> Bart writes: >>> [...] >>> >>>> Take: >>>> >>>> int a; double x; >>>> >>>> x = (double)a; >>>> >>>> The cast is implicit here but I've written it out to make it clear. >>> >>> [...] >>> >>> The *conversion* could be done implicitly, but you've used a cast (i.e., >>> an explicit conversion) to make it clear. >> The statement assigning to x performs two conversions: an explicit >> one caused by the cast, and an implicit one caused by the assignment >> operation. > > The 'x' term is the other side of the cast from the 'a' term. > > So after '(double)a' has been evaluated, both sides of '=' have the > type 'double', so no further conversion is needed. Bart, I'm sure you don't care about this, but others might. Please do us all a favor and don't argue about it. The standard says that "In *simple assignment* (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand." This is unambiguous, and for your example it means that the result of the evaluating the RHS is *converted* from double to double. "Conversion of an operand value to a compatible type causes no change to the value or the representation." double is compatible with itself, so you presumably won't find any evidence of a conversion if you examine the generated code. The standard could have described this differently, perhaps by saying that no conversion is performed if the LHS and RHS have the same type, but it says what it says. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */