Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Dr Nick <3-nospam@temporary-address.org.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: flowto (Just for fun) |
| Date | 2011-07-10 12:15 +0100 |
| Message-ID | <87oc12z8rg.fsf@temporary-address.org.uk> (permalink) |
| References | (3 earlier) <9e27aa89-740f-415d-90e4-33c1e977485b@q1g2000vbj.googlegroups.com> <87sjqe3o58.fsf@bazspaz.fatphil.org> <slrn3vfsj1iq03.5fi.ike@sverige.freeshell.org> <87wrfqzcm3.fsf@temporary-address.org.uk> <slrn3vfsj1j29b.7t3.ike@sverige.freeshell.org> |
Ike Naar <ike@sverige.freeshell.org> writes:
> On 2011-07-10, Dr Nick <3-nospam@temporary-address.org.uk> wrote:
>> Ike Naar <ike@sverige.freeshell.org> writes:
>>
>>> On 2011-07-10, Phil Carmody <thefatphil_demunged@yahoo.co.uk> wrote:
>>>> gwowen <gwowen@gmail.com> writes:
>>>>> On Jul 8, 12:39?am, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
>>>>> wrote:
>>>>>
>>>>> > However, making it a solitary enum just to avoid making it a #define is
>>>>> > nothing more than sophistry.
>>>>>
>>>>> Scoping is not sophistry.
>>>>
>>>> You can make #defines disappear just as easily as enums, were that
>>>> to be an issue.
>>>
>>> Actually, it can be a bit tricky to do it well.
>>> Can you demonstrate how easily the enums can be replaced by #defines
>>> in the program below?
>>>
>>> #include <assert.h>
>>>
>>> enum {value = 0};
>>> int global = value;
>>>
>>> void foo(int, int);
>>>
>>> int main(void)
>>> {
>>> enum {value = 1};
>>> foo(global, value);
>>> return 0;
>>> }
>>>
>>> void foo(int i0, int i1)
>>> {
>>> assert(i0 == value && i1 == 1);
>>> }
>>
>> Sure:
>>
>> #define GLOBAL_DEFAULT 0
>> #define APPROPRIATE_VALUE 1
>>
>> #include <assert.h>
>>
>> int global = GLOBAL_DEFAULT;
>>
>> void foo(int, int);
>>
>> int main(void)
>> {
>> foo(global, APPROPRIATE_VALUE);
>> return 0;
>> }
>>
>> void foo(int i0, int i1)
>> {
>> assert(i0 == value && i1 == 1);
>> }
>>
>> A heck of a lot less obfuscated as well!
>
> Names were changed, and a local entity (``value'' inside main) was
> moved to the global level. That's more than merely using #defines
> instead of enums.
>
> The small example may look obfuscated, but in a large program you
> want to be able to use local names (e.g. restricted to a function
> body) without running the risk of breaking things globally.
>
> Your solution might break code that uses the name APPROPRIATE_VALUE
> for other purposes.
If it's small and local and never used anywhere else then you might as
well just have it as an explicit naked value (with an explanatory
comment) rather than carefully creating an enum just to give it a name
for a one-off use right next to where it's defined.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Re: flowto (Just for fun) luser- -droog <mijoryx@yahoo.com> - 2011-07-07 00:03 -0700
Re: flowto (Just for fun) Dr Nick <3-nospam@temporary-address.org.uk> - 2011-07-07 08:06 +0100
Re: flowto (Just for fun) Shao Miller <sha0.miller@gmail.com> - 2011-07-07 19:34 -0400
Re: flowto (Just for fun) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-08 02:39 +0300
Re: flowto (Just for fun) Keith Thompson <kst-u@mib.org> - 2011-07-07 17:09 -0700
Re: flowto (Just for fun) gwowen <gwowen@gmail.com> - 2011-07-08 03:10 -0700
Re: flowto (Just for fun) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-10 04:44 +0300
Re: flowto (Just for fun) Ike Naar <ike@sverige.freeshell.org> - 2011-07-10 08:51 +0000
Re: flowto (Just for fun) Dr Nick <3-nospam@temporary-address.org.uk> - 2011-07-10 10:52 +0100
Re: flowto (Just for fun) Ike Naar <ike@sverige.freeshell.org> - 2011-07-10 11:12 +0000
Re: flowto (Just for fun) Dr Nick <3-nospam@temporary-address.org.uk> - 2011-07-10 12:15 +0100
Re: flowto (Just for fun) Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-07-11 03:32 -0700
Re: flowto (Just for fun) Dr Nick <3-nospam@temporary-address.org.uk> - 2011-07-11 20:11 +0100
Re: flowto (Just for fun) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-10 20:42 +0300
Re: flowto (Just for fun) "io_x" <a@b.c.invalid> - 2011-07-11 08:08 +0200
Re: flowto (Just for fun) luser- -droog <mijoryx@yahoo.com> - 2011-07-10 23:51 -0700
Re: flowto (Just for fun) "io_x" <a@b.c.invalid> - 2011-07-11 09:15 +0200
Re: flowto (Just for fun) "io_x" <a@b.c.invalid> - 2011-07-08 20:06 +0200
csiph-web