[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: flowto (Just for fun

luserXtrog

7/7/2011 7:04:00 AM

On Saturday, July 2, 2011 1:58:27 AM UTC-5, io_x wrote:
> "Shao Miller" <sha0....@gmail.com> ha scritto nel messaggio
> news:iul55q$a22$1@dont-email.me...
> i prefer- and all goes well- C macro if without arg
> something as
> #define nome1 0x123

This is better as an enum
enum { nome1 = 0x123 };

> #define u32 unsigned int

This is better as a typedef
typedef unsigned int u32;

> etc

etc.

> possibily not use the ones with arg for some good reason
> e.g. debugging

If you're as clever as possible when you program,
how will you ever debug it?
-K
18 Answers

Nick

7/7/2011 7:07:00 AM

0

luser- -droog <mijoryx@yahoo.com> writes:

> On Saturday, July 2, 2011 1:58:27 AM UTC-5, io_x wrote:
>> "Shao Miller" <sha0....@gmail.com> ha scritto nel messaggio
>> news:iul55q$a22$1@dont-email.me...
>> i prefer- and all goes well- C macro if without arg
>> something as
>> #define nome1 0x123
>
> This is better as an enum
> enum { nome1 = 0x123 };

Very debatable. I really don't like the use of enums just as isolated
symbolic constants, and I don't think I'm alone in this.
--
Online waterways route planner | http://ca...
Plan trips, see photos, check facilities | http://canalp...

Shao Miller

7/7/2011 11:35:00 PM

0

On 7/7/2011 03:06, Dr Nick wrote:
> luser- -droog<mijoryx@yahoo.com> writes:
>
>> On Saturday, July 2, 2011 1:58:27 AM UTC-5, io_x wrote:
>>> "Shao Miller"<sha0....@gmail.com> ha scritto nel messaggio
>>> news:iul55q$a22$1@dont-email.me...
>>> i prefer- and all goes well- C macro if without arg
>>> something as
>>> #define nome1 0x123
>>
>> This is better as an enum
>> enum { nome1 = 0x123 };
>
> Very debatable. I really don't like the use of enums just as isolated
> symbolic constants, and I don't think I'm alone in this.

Some folks have expressed that 'enum's are only good for 'switch'ing on.
I've even seen the following argument against any other use:

If you can't define constants of all types without using macros,
don't define any constants without using macros.

An argument about consistency, I think.

I like 'enum's for fits-in-'int' values and if the day ever comes that
there's a portable way to avoid macros for other types of constants,
I'll use those, too. :)

Phil Carmody

7/7/2011 11:40:00 PM

0

Dr Nick <3-nospam@temporary-address.org.uk> writes:
> luser- -droog <mijoryx@yahoo.com> writes:
>
> > On Saturday, July 2, 2011 1:58:27 AM UTC-5, io_x wrote:
> >> "Shao Miller" <sha0....@gmail.com> ha scritto nel messaggio
> >> news:iul55q$a22$1@dont-email.me...
> >> i prefer- and all goes well- C macro if without arg
> >> something as
> >> #define nome1 0x123
> >
> > This is better as an enum
> > enum { nome1 = 0x123 };
>
> Very debatable. I really don't like the use of enums just as isolated
> symbolic constants, and I don't think I'm alone in this.

You are not alone. I abhor magic numbers. If there's any chance that a
number will be used twice, it should be some kind of symbolic constant.
However, making it a solitary enum just to avoid making it a #define is
nothing more than sophistry.

Phil
--
"At least you know where you are with Microsoft."
"True. I just wish I'd brought a paddle." -- Matthew Vernon

Keith Thompson

7/8/2011 12:10:00 AM

0

Phil Carmody <thefatphil_demunged@yahoo.co.uk> writes:
> Dr Nick <3-nospam@temporary-address.org.uk> writes:
>> luser- -droog <mijoryx@yahoo.com> writes:
>>
>> > On Saturday, July 2, 2011 1:58:27 AM UTC-5, io_x wrote:
>> >> "Shao Miller" <sha0....@gmail.com> ha scritto nel messaggio
>> >> news:iul55q$a22$1@dont-email.me...
>> >> i prefer- and all goes well- C macro if without arg
>> >> something as
>> >> #define nome1 0x123
>> >
>> > This is better as an enum
>> > enum { nome1 = 0x123 };
>>
>> Very debatable. I really don't like the use of enums just as isolated
>> symbolic constants, and I don't think I'm alone in this.
>
> You are not alone. I abhor magic numbers. If there's any chance that a
> number will be used twice, it should be some kind of symbolic constant.
> However, making it a solitary enum just to avoid making it a #define is
> nothing more than sophistry.

I disagree; it's considerably more than sophistry.

It's a use of the "enum" feature for a purpose for which it was
probably never intended. But it *works*, in the sense that it makes
a specified identifier usable as a constant expression of type int.

If I were looking for complete purity and consistency, I'd likely
decide that C is not the language for me.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

gwowen

7/8/2011 10:11:00 AM

0

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.

io_x

7/8/2011 6:06:00 PM

0


"luser- -droog" <mijoryx@yahoo.com> ha scritto nel messaggio
news:1b6e1f81-b774-4ddc-9af4-68607dbed1a7@glegroupsg2000goo.googlegroups.com...
> On Saturday, July 2, 2011 1:58:27 AM UTC-5, io_x wrote:
>> "Shao Miller" <sha0....@gmail.com> ha scritto nel messaggio
>> news:iul55q$a22$1@dont-email.me...
>> i prefer- and all goes well- C macro if without arg
>> something as
>> #define nome1 0x123
>
> This is better as an enum
> enum { nome1 = 0x123 };
>
>> #define u32 unsigned int
>
> This is better as a typedef
> typedef unsigned int u32;
>
>> etc
>
> etc.
>
>> possibily not use the ones with arg for some good reason
>> e.g. debugging
>
> If you're as clever as possible when you program,
> how will you ever debug it?
> -K

because i know i'm not so clever so i have to debug all
i write




Phil Carmody

7/10/2011 1:45:00 AM

0

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.

Phil
--
"At least you know where you are with Microsoft."
"True. I just wish I'd brought a paddle." -- Matthew Vernon

Ike Naar

7/10/2011 8:51:00 AM

0

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);
}

Nick

7/10/2011 9:53:00 AM

0

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!
--
Online waterways route planner | http://ca...
Plan trips, see photos, check facilities | http://canalp...

Ike Naar

7/10/2011 11:13:00 AM

0

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.