Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #150296
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: clarification about enums in C |
| Date | 2020-02-27 14:57 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <r38hsr$fcs$1@dont-email.me> (permalink) |
| References | (3 earlier) <aafdcd8f-ba2e-4237-9df9-c9808a34b78f@googlegroups.com> <r384n9$j0c$1@dont-email.me> <66b4b494-68c6-420a-b0e3-c4cb4b357d9b@googlegroups.com> <r38gfh$1a9$1@dont-email.me> <2e1f4ca8-2045-4d4f-bdda-426cec6b44f7@googlegroups.com> |
On 27/02/2020 14:44, already5chosen@yahoo.com wrote:
> On Thursday, February 27, 2020 at 3:33:45 PM UTC+2, David Brown wrote:
>> On 27/02/2020 12:09, already5chosen@yahoo.com wrote:
>>> On Thursday, February 27, 2020 at 12:13:06 PM UTC+2, David Brown wrote:
>>>> On 27/02/2020 10:29, already5chosen@yahoo.com wrote:
>> <snip>
>>
>>>>>
>>>>> In case of Windows port of gcc and clang, even 'long long int',
>>>>> This case is especially funny:
>>>>>
>>>>> enum en_t {
>>>>> A = 1,
>>>>> B = -1,
>>>>> C = (unsigned)-1,
>>>>> };
>>>>>
>>>>> type of en_t is 'long long'
>>>>
>>>> Yes, that makes sense (as an extension)
>>>
>>> It makes non-zero sense, but not a whole lot of it.
>>> I'd very much prefer a different language extension as solution to the same practical problem: a keyword __const, with semantics identical to C++ keyword const.
>>> With a hope of inclusion into next revision of the standard.
>>>
>>
>> While I too would like a C++ style "const" in C, I think "enum" is a
>> different situation. Sometimes people use "enum" simply to make
>> independent constants that in C++ can be made with "const int A = 1;".
>> In C++, you can use a "const int" like that for the size of an array, or
>> for initialising other file-scope objects - in C you can't, so you use
>> either "enum { A = 1 };" or "#define A 1".
>>
>> However, a big use of "enum" is to group a collection of states or
>> values - I use it as an enumerated type. (C++ improves on this (for
>> many uses) with enum classes.) Compilers and static analysis can do
>> additional checks with things like switch cases or warning about legal
>> but probably incorrect assignments or operations on enumerated types.
>> Debuggers can show values as text, rather than as a number. You can't
>> get any of that with a bunch of "const int" declarations.
>>
>
> But for this use of enum 'int' range, specified in the Standard, appears fully adequate.
> So, if it remains the only use, I see no need for extension of the range beyond 'int'.
>
Well, yes. However, there are more uses for enum. An example is for
flags that you want to combine:
enum colour {
black = 0,
red = 1,
blue = 2,
green = 4,
magenta = red + blue,
cyan = blue + green,
yellow = green + red,
white = red + blue + green
};
With bigger sets of flags, you might easily go beyond the size of "int".
Generally speaking, "enum" is the C way to group a bunch of named
constant integer values. While "int" is big enough for most uses, it
seems arbitrary to restrict it to "int" when it is so obvious and simple
to extend it.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
clarification about enums in C Thiago Adams <thiago.adams@gmail.com> - 2020-02-25 15:38 -0800
Re: clarification about enums in C luser droog <luser.droog@gmail.com> - 2020-02-25 19:19 -0800
Re: clarification about enums in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-25 21:17 -0800
Re: clarification about enums in C Thiago Adams <thiago.adams@gmail.com> - 2020-02-27 04:01 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-26 11:29 +0100
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-26 14:05 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-26 23:03 +0000
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-26 23:29 -0800
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 01:42 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 11:02 +0100
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-27 06:37 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 16:18 +0100
Re: clarification about enums in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 23:24 -0500
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 09:50 +0100
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-27 06:33 -0800
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 06:53 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 15:05 +0000
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 16:24 +0100
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-28 01:27 -0800
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 01:29 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 11:12 +0100
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 03:09 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 14:33 +0100
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 05:44 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 14:57 +0100
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 11:53 +0000
Re: clarification about enums in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 23:52 -0500
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-28 01:30 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-26 14:09 +0000
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-26 09:47 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-26 21:04 +0000
csiph-web