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


Groups > comp.lang.c > #89185

Re: efficient coding with literals and variables

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: efficient coding with literals and variables
Date 2016-06-15 11:47 -0700
Organization None to speak of
Message-ID <ln37oez3v4.fsf@kst-u.example.com> (permalink)
References <literals-20160614201332@ram.dialup.fu-berlin.de> <njpnbq$je8$1@dont-email.me> <njqucm$1e9$1@dont-email.me> <lnk2hqz8qh.fsf@kst-u.example.com> <njs4c3$dev$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:
> On 15/06/16 19:01, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 14/06/16 21:53, BartC wrote:
>> [...]
>>>> (I'm not sure I would bother either: if these are constant values known
>>>> at compile time, then #define or enums are better ways of denoting a
>>>> named constant.)
>>>
>>> No, #define and enum are /not/ better ways of denoting named constants.
>>>   They are sometimes the only way to get what you need, especially in C
>>> (in C++, "const int" is more powerful than it is in plain C).  It's okay
>>> to say that you /prefer/ to use #define or enum for named constants -
>>> but don't make the mistake of thinking your preferences for an extremely
>>> simple, limited and flat-structured language is "better" for other
>>> people or other purposes.
>>
>> The problem with const is that it doesn't create a constant expression.
>> enum does (though it's limited to constants of type int).
>
> Yes, and that is an annoying limitation (C++ improves on that) - which 
> is why I said that #define and enum are "sometimes the only way to get 
> what you need".  But in other aspects, "const" objects are better - in 
> particular, they obey the scoping and parsing rules of C rather than the 
> pre-processor, and they have a proper type (rather than being sort-of 
> int, sort-of their own enumeration type for enums).

Enumeration constants are not sort-of anything.  They're of type int.

-- 
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 | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: efficient coding with literals and variables BartC <bc@freeuk.com> - 2016-06-14 20:53 +0100
  Re: efficient coding with literals and variables David Brown <david.brown@hesbynett.no> - 2016-06-15 08:59 +0200
    Re: efficient coding with literals and variables Keith Thompson <kst-u@mib.org> - 2016-06-15 10:01 -0700
      Re: efficient coding with literals and variables David Brown <david.brown@hesbynett.no> - 2016-06-15 19:47 +0200
        Re: efficient coding with literals and variables Keith Thompson <kst-u@mib.org> - 2016-06-15 11:47 -0700
    Re: efficient coding with literals and variables Ike Naar <ike@iceland.freeshell.org> - 2016-06-15 21:23 +0000
      Re: efficient coding with literals and variables David Brown <david.brown@hesbynett.no> - 2016-06-15 23:53 +0200
        Re: efficient coding with literals and variables Ike Naar <ike@iceland.freeshell.org> - 2016-06-15 22:41 +0000
          Re: efficient coding with literals and variables "James R. Kuyper" <jameskuyper@verizon.net> - 2016-06-15 18:54 -0400
            Re: efficient coding with literals and variables Ike Naar <ike@iceland.freeshell.org> - 2016-06-16 05:41 +0000
          Re: efficient coding with literals and variables David Brown <david.brown@hesbynett.no> - 2016-06-16 09:12 +0200
      Re: efficient coding with literals and variables Keith Thompson <kst-u@mib.org> - 2016-06-15 14:53 -0700

csiph-web