Deutsch   English   Français   Italiano  
<87bjxevm35.fsf@nosuchdomain.example.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: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: C89 "bug"
Date: Fri, 13 Dec 2024 22:02:22 -0800
Organization: None to speak of
Lines: 41
Message-ID: <87bjxevm35.fsf@nosuchdomain.example.com>
References: <vjh8hu$3den0$1@dont-email.me> <vjj63a$24no4$2@paganini.bofh.team>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sat, 14 Dec 2024 07:02:31 +0100 (CET)
Injection-Info: dont-email.me; posting-host="f020b395aa19c836e8bce37085be125e";
	logging-data="4083410"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18eChSlixB3wZuu++ecN0MW"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:q2h12mt2cLYJLec/M85O6IjUqDw=
	sha1:xgxAqpM166cd5KRMR20Xj+GhmMc=
Bytes: 1973

antispam@fricas.org (Waldek Hebisch) writes:
> Thiago Adams <thiago.adams@gmail.com> wrote:
>> Does anyone knows how can I convert this code (external declaration) to C89?
>> 
>> union U {
>>     int i;
>>     double d;
>> };
>> 
>> union U  u = {.d=1.2};
>> 
>> The problem is that in C89 only the first member of the union is 
>> initialized.
>
> The following:
>
> union U {
>     int i;
>     double d;
> };
>
> union U u = (union U)(1.2);
>
> is accepted by 'gcc -S -std=c90'.  I leave to others to check
> if this is valid C89.

It's not.  With "-std=c90 -pedantic", gcc (correctly) warns:

    ISO C forbids casts to union type [-Wpedantic]

The specific constraint in the C90 standard is:

    Unless the type name specifies void type, the type name shall
    specify qualified or unqualified scalar type and the operand
    shall have scalar type.

(Later editions have similar wording.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */