Path: csiph.com!1.us.feeder.erje.net!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Are there any conformant C compilers? Date: Wed, 31 Aug 2022 11:39:22 -0700 Organization: None to speak of Lines: 59 Message-ID: <87a67ki7fp.fsf@nosuchdomain.example.com> References: <87zgfqk9l4.fsf@nosuchdomain.example.com> <413ae987-3216-415f-88cf-8a6a0f7cfe4an@googlegroups.com> <90aaf37e-e48d-40c4-ad98-bf34533af2ben@googlegroups.com> <871qswsdn2.fsf@bsb.me.uk> <87h71spdn3.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="257379dd7a173905873229f2750bce60"; logging-data="1984524"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ArzBOHwAzDMCojRLq12sB" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:qVwZta76HVUOHbOTv9rudKE/tko= sha1:doda/SbHdapxBXukc/0qL8U9Yxs= Xref: csiph.com comp.lang.c:167397 Bart writes: > On 31/08/2022 17:43, Ben Bacarisse wrote: >> Bart writes: > >>> const int abc = 123; >>> *(int*)&abc = 999; >>> >>> printf("abc = %d\n", abc); >>> >>> It tells me that 'abc' is 999; so much for being a named constant, or >>> read-only! >> Don't you have a compiler that will tell you that such code is junk? >> (It won't say so in so many words. Compiler writers are more polite >> than I am!) > > Apparently not: > > c:\c>gcc c.c -oc.exe > c:\c>c > 999 > > c:\c>tcc c.c > c:\c>c > 999 Compilers typically don't warn about pointer casts. A pointer cast effectively tells the compiler "I know this isn't necessarily the right type, but I know what I'm doing". The code, of course, has undefined behavior. With optimization enabled, on my system, the output is "123". > With this one I do get a message: > > c:\c>bcc c > Compiling c.c to c.exe > In function main > Type error: Modifying read-only var on line 6 c.c > > But that's most likely a compiler error or just non-conforming. If I > pull out the stops with gcc, it gives me a warning, but I can still > run the program. The code does not violate a constraint. If bcc's message is a non-fatal warning, there's no problem. If it's a fatal error, bcc could still be conforming, since one of the allowed consequences of undefined behavior is "... terminating a translation or execution (with the issuance of a diagnostic message)". If bcc rejects the code when it can't prove that it will always be executed, it might be non-conforming -- as most C compilers are by default. [...] C has always allowed programmers to do silly things. Don't do that. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */