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


Groups > comp.lang.c > #120549

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-09-29 09:10 -0700
Organization None to speak of
Message-ID <ln60c1v625.fsf@kst-u.example.com> (permalink)
References <89577a10-c851-42ee-a68a-825a25baded0@googlegroups.com> <oql21t$mh6$1@dont-email.me> <2c5a4b5b-ec8b-44fe-a77c-e26a0e1c5bb9@googlegroups.com> <NPrzB.979054$gM6.814414@fx03.am4>

Show all headers | View raw


bartc <bc@freeuk.com> writes:
> On 29/09/2017 14:06, Thiago Adams wrote:
>> On Friday, September 29, 2017 at 5:59:53 AM UTC-3, David Brown wrote:
>
>>> There is already "static const int C = 1;".  The compiler will usually
>>> avoid giving this storage space, unless you take its address.  (C++17
>>> added "inline variables" for a somewhat different reason.  It would IMHO
>>> be a bad idea to add inline variables to C that are incompatible with
>>> the C++ versions.)
>> 
>> Did you notice everyone use #define for constants?
>> C programmers could also use enuns but they use enum only
>> when the values are enumerated.
>> enum {CONST1 = 1};
>> 
>> For all other cases they use #define (in general)
>> I think the reason (I can ask for those are reading this)
>> is because we don't feel comfortable using const int i = 1;
>> and hopping that his will not be placed at storage.

It's not about hoping that it isn't placed in storage.  The storage for
a single int object is trivial in almost all contexts, and we already
rely on compilers to optimize away storage when it's not needed.

The problem is that given
    const int foo = 1;
the expression foo is not a constant expression.

>> #define CONST1 1
>> 
>> give us what we want.

And given that macro definition, CONST1 is a constant expression.

>> And I think they don't use enuns because they are an special case
>> for ints and they don't want to have two styles then they
>> use #define for ints also.

Using enums to define constants of type int is a fairly common
technique, but not everyone uses it.  And the fact that it's limited to
type int is annoying.

Another advantage of #define is that CONST1 can be used in a
preprocessor expression; an enumeration constant cannot.

> I think you're the only person ever to have voiced the same misgivings 
> about C's failings in this area as I have.

No, a lot of people have discussed it at great length, myself included.
I've advocated adopting something similar to what C++ does, so that
defining a const object initialized with a constant expression causes
the name of the object to be a constant expression.

> The language solution for this, at least for numeric types, is very, 
> very simple; it is to have named constants, eg:
>
> constant const1 1;    // defaults to type of the expression, here int
>
> So that 'const1' will subsequently work EXACTLY the same as writing a 
> literal '1' in the code.

Apart from scoping, #define already does that.

[...]

-- 
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