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


Groups > comp.lang.c > #120701

Re: const and the C type system

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: const and the C type system
Date 2017-10-02 09:40 -0700
Organization None to speak of
Message-ID <lnbmlpsdtp.fsf@kst-u.example.com> (permalink)
References (9 earlier) <gFcAB.1102930$LJ4.573616@fx37.am4> <lno9pqshgg.fsf@kst-u.example.com> <XmeAB.1303974$ct3.74866@fx32.am4> <lnk20es3fs.fsf@kst-u.example.com> <CEoAB.1232842$tN5.699712@fx27.am4>

Show all headers | View raw


bartc <bc@freeuk.com> writes:
> On 02/10/2017 03:12, Keith Thompson wrote:
>> bartc <bc@freeuk.com> writes:
>>> OK, I understand: this is a problem at file-scope. And it's the same
>>> problem that stops you using such a value for a fixed array bound or for
>>> a case-label.
>> 
>> Do you understand that the initializer for a static object must be a
>> constant expression either at file scope or at block scope?  You say you
>> didn't get a complaint from gcc.  I'm skeptical.  Did you try that exact
>> code, or did you omit the "static" keyword?  Please try it again with
>> the exact code that you posted above.
>
> c:\c>type c.c
> #include <stdio.h>
>
> int main(void){
>    static const int a = 100;
>    static const double b = 200.1;
>    static const double c = a+b;
>    static const long long int d = 5000000000;
>    static const double e = d*c;
>    static const unsigned long long f = 0xffffffffffffffff;
>
>    printf("a=%d\n",a);
>    printf("b=%f\n",b);
>    printf("c=%f\n",c);
>    printf("d=%lld\n",d);
>    printf("e=%f\n",e);
>    printf("f=%llX\n",f);
> }
>
> c:\c>\tdm\bin\gcc -O3 c.c
>
> The problem seems to be the -O3 option. Without it, gcc will complain as 
> you say. So it's a gcc problem.
>
> No doubt you guys will again try to twist things around and say that 
> it's a Bart problem,

No, it appears to be a bug in gcc.

C11 6.10p10 says:
    An implementation may accept other forms of constant expressions.
If gcc were taking advantage of that permission, the lack of a
diagnostic would necessarily be a conformance issue.  But the fact that
the behavior changes with optimization level make me think it's not a
deliberate feature.

So I think it's a bug in gcc; it's failing to diagnose a constraint
violation.  The commands you've shown don't actually demonstrate
a bug since gcc is not conforming by default, but when I compile
the same source with "gcc -std=c11 -pedantic -O3", it still fails
to produce a diagnostic.

>                      but the fact reminds that with -O3, nothing was 
> said, so that I genuinely thought a,b,c,d,e,f were all just synonyms for 
> their values. If I specify this lot, it still says nothing about it 
> (I've no idea what those format warnings are about):

OK, given that gcc didn't complain, it was probably reasonable for you
to assume that the code was valid.

> c:\c>\tdm\bin\gcc -O3 -std=c11 -Wall -Wextra -Wpedantic -c c.c
>
> c.c: In function 'main':
> c.c:14:10: warning: unknown conversion type character 'l' in format
>             [-Wformat=]
>     printf("d=%lld\n",d);
>            ^
> c.c:14:10: warning: too many arguments for format [-Wformat-extra-args]
> c.c:16:10: warning: unknown conversion type character 'l' in format
>             [-Wformat=]
>     printf("f=%llX\n",f);
>            ^
> c.c:16:10: warning: too many arguments for format [-Wformat-extra-args]

That's weird.  What version of gcc are you using ("gcc --version")?

gcc 6.3.0 doesn't complain about the format strings.  An very
old version of gcc might not recognize the "%lld" format, which
was introduced in C99, but then it shouldn't have recognized the
"-std=c11" option.

>> The point is that static objects are created and initialized before
>> main() begins, and C has no mechanism for running code at that point.
>
> So how does gcc manage it with -O3? The above program worked.

Some expressions that are not constant expressions (as defined by the C
standard) can still be evaluated at compile time.  The value of a
const-qualfied object with a constant initializer is one such
expression.  A conforming compiler can generate code that reduces the
value of such an object to a constant, but it must still diagnose any
use of the name of such an object in a context that requires a constant
expression.  I suspect that gcc is internally failing to distinguish
between language-defined constant expressions and expressions that it
can evaluate at compile time; it's incorrectly discarding information
that it needs so it can produce a diagnostic.

[...]

>> Knowing that "const" doesn't mean "constant" might have prevented that
>> misunderstanding.
>
> THIS IS THE WHOLE POINT! You're TRYING to use 'const' to implement 
> something equivalent to (my) 'constant'.

I meant knowing that "const" doesn't mean "constant" *in C as it's
currently defined*.  For purposes of your test program, we're talking
about the requirements of C11, not of any proposed future changes.

C++ already uses "const" to implement something similar to your
proposed "constant".  As I've already said, the C++ feature blurs
the distinction between "const" and "constant", which is in my
opinion a drawback.  In spite of that, I think it's a worthwhile
addition just because it's useful functionality.  On the other
hand, I think that a limited form of C++'s more recent "constexpr"
might be a better solution (and, with the limitations I'm thinking
of, should be no more difficult to implement than your proposal).
Quite possibly if "constexpr" had been added to C++ sooner, there
would have been no need for it to change the semantics of "const".

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-28 19:51 -0700
  Re: const and the C type system Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-09-28 21:32 -0600
    Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 05:45 -0700
      Re: const and the C type system bartc <bc@freeuk.com> - 2017-09-29 14:05 +0100
        Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 06:17 -0700
    Re: const and the C type system asetofsymbols@gmail.com - 2017-10-01 22:35 -0700
  Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-09-29 10:59 +0200
    Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 06:06 -0700
      Re: const and the C type system bartc <bc@freeuk.com> - 2017-09-29 14:24 +0100
        Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 06:52 -0700
          Re: const and the C type system jameskuyper@verizon.net - 2017-09-29 09:17 -0700
            Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 10:50 -0700
              Re: const and the C type system jameskuyper@verizon.net - 2017-09-29 11:06 -0700
                Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 11:27 -0700
          Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-30 13:13 -0700
            Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-30 13:31 -0700
        Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-09-29 09:10 -0700
          Re: const and the C type system bartc <bc@freeuk.com> - 2017-09-29 18:33 +0100
          Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 11:10 -0700
        Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-09-30 08:29 +1300
        Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-09-30 18:48 +0200
          Re: const and the C type system bartc <bc@freeuk.com> - 2017-09-30 18:44 +0100
            Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-10-01 09:12 +1300
              Re: const and the C type system bartc <bc@freeuk.com> - 2017-09-30 21:50 +0100
                Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-10-01 10:10 +1300
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-09-30 23:17 +0100
                Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-10-01 11:38 +1300
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 00:44 +0100
                Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-10-01 14:33 +1300
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 10:42 +0100
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 19:58 +0200
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 21:30 +0100
                Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-10-02 17:48 +1300
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-02 11:14 +0100
                Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-10-02 23:22 +1300
                Re: const and the C type system Richard Damon <Richard@Damon-Family.org> - 2017-10-01 13:58 -0400
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 18:20 +0200
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 18:41 +0100
                Re: const and the C type system supercat@casperkitty.com - 2017-10-01 12:03 -0700
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 23:05 +0200
                Re: const and the C type system supercat@casperkitty.com - 2017-10-01 15:58 -0700
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-02 09:56 +0200
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-02 00:24 +0100
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-02 12:20 +0200
                Re: const and the C type system Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-10-02 06:03 -0700
                Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-10-02 06:34 -0700
                Re: const and the C type system supercat@casperkitty.com - 2017-10-02 23:38 -0700
                Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-10-01 13:03 -0700
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 23:12 +0200
                Re: const and the C type system supercat@casperkitty.com - 2017-10-01 11:12 -0700
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 13:48 +0200
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 14:13 +0100
                Re: const and the C type system supercat <flatfinger@casperkitty.com> - 2017-10-01 10:32 -0700
              Re: const and the C type system supercat@casperkitty.com - 2017-09-30 13:52 -0700
            Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-09-30 13:35 -0700
              Re: const and the C type system bartc <bc@freeuk.com> - 2017-09-30 21:58 +0100
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 12:03 +0100
              Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-30 14:00 -0700
                Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-09-30 15:33 -0700
                Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-10-01 12:45 -0700
            Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 13:13 +0200
              Re: const and the C type system Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-10-01 05:22 -0700
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 20:06 +0200
                Re: const and the C type system supercat@casperkitty.com - 2017-10-01 12:16 -0700
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 21:01 +0100
                Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-10-01 13:21 -0700
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 21:50 +0100
                Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-10-02 05:35 -0700
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-02 14:01 +0100
                Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-10-02 06:29 -0700
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-02 15:19 +0100
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 23:19 +0200
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 22:33 +0100
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-02 00:10 +0200
              Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 14:13 +0100
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-01 22:46 +0200
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 21:57 +0100
                Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-10-01 14:09 -0700
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-01 23:54 +0100
                Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-10-01 19:12 -0700
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-02 11:36 +0100
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-02 12:45 +0200
                Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-10-02 09:40 -0700
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-02 19:33 +0100
                Re: const and the C type system scott@slp53.sl.home (Scott Lurndal) - 2017-10-02 19:09 +0000
                Re: const and the C type system Ian Collins <ian-news@hotmail.com> - 2017-10-03 08:12 +1300
                Re: const and the C type system bartc <bc@freeuk.com> - 2017-10-02 20:30 +0100
                Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-10-02 12:37 -0700
                Re: const and the C type system jadill33@gmail.com - 2017-10-02 12:49 -0700
                Re: const and the C type system Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-10-06 20:47 +0000
                Re: const and the C type system Keith Thompson <kst-u@mib.org> - 2017-10-06 15:41 -0700
                Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-10-02 12:42 +0200
              Re: const and the C type system Gareth Owen <gwowen@gmail.com> - 2017-10-01 21:18 +0100
          Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-30 12:22 -0700
      Re: const and the C type system David Brown <david.brown@hesbynett.no> - 2017-09-30 18:44 +0200
    Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 07:01 -0700
      Re: const and the C type system Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-09-29 20:18 +0100
        Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-09-29 12:40 -0700
          Re: const and the C type system Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-09-29 21:28 +0100
  Re: const and the C type system fir <profesor.fir@gmail.com> - 2017-09-29 08:21 -0700
    Re: const and the C type system fir <profesor.fir@gmail.com> - 2017-09-29 08:38 -0700
  Re: const and the C type system John Bode <jfbode1029@gmail.com> - 2017-10-02 09:20 -0700
    Re: const and the C type system Thiago Adams <thiago.adams@gmail.com> - 2017-10-02 09:53 -0700
    Re: const and the C type system Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-10-09 13:27 +0000

csiph-web