Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

enum and define

Name: Anonymous 2010-09-25 12:15

So tell me, /prog/rammers

In C, why use enum instead of just #define?

Name: Anonymous 2010-09-25 12:17

mmmm, i dont knoiw, but i use define for constants and enum for sets (as in Sets Theory).

For me, the difference is conceptual

Name: Anonymous 2010-09-25 12:18

I'm not even sure how you would use #define to replace

typedef int BOOL;
enum{FALSE, TRUE};

Name: Anonymous 2010-09-25 12:21

You'd

#define FALSE 0
#define TRUE 1

but I couldn't tell you how to assign that to BOOL

Name: Anonymous 2010-09-25 12:23

Because you can write

enum
{
    A = 1,
    B,
    C,
    D
}


instead of

#define A 1
#define B 2
#define C 3
#define D 4

Name: Anonymous 2010-09-25 12:26

>>4

#define FALSE 0
#define TRUE !FALSE

Name: Anonymous 2010-09-25 12:26

debug symbols

Name: Anonymous 2010-09-25 12:29

OP here: So enum is essentially just one-step defines, then?

Is there a way to get it to go in bigger steps, e.g.

#define a 0
#define b 1
#define c 2
#define d 4
#define e 8

etc.?

Or even size steps, like

#define a 0
#define b 2
#define c 4
#define d 6
#define e 8

?

Name: Anonymous 2010-09-25 12:31

>>8
Its called the + operator.

Name: Anonymous 2010-09-25 12:39

>>8 Dont ask too much from C. You want higher level features?  Use a higher level language.

Name: Anonymous 2010-09-25 13:20

I use them for this:

[0][~]% cat enum.c
enum e_num
{
        ENUM_CASE_1,
        ENUM_CASE_2,
        ENUM_CASE_3,
        ENUM_CASE_4
};

void f()
{
        enum e_num e = ENUM_CASE_3;
        switch(e){
                case ENUM_CASE_1:
                case ENUM_CASE_2:
                case ENUM_CASE_3:
                        break;
        }
}

[0][~]% gcc -S -o /dev/null -Wall enum.c
enum.c: In function ‘f’:

enum.c:12:2: warning: enumeration value ‘ENUM_CASE_4’ not handled in switch


[0][~]%

Name: Anonymous 2010-09-25 18:48

>>11
[0][~]%
what's that?

Name: Anonymous 2010-09-25 18:56

>>12
A zero in brackets, a tilde in brackets and a percent sign.

Name: Anonymous 2010-09-25 19:33

>>12
Usually it's: [return code][pwd]prompt

Name: Anonymous 2010-09-25 19:50

Because enumeration constants do the same job (for int constants) and are far less error prone.

Name: Anonymous 2010-09-25 20:10

The only major advantage I have noticed is when using code-completion (eg in Visual Studio)

Name: Anonymous 2010-09-25 21:28

Type safety, the fact that it's less typing even in degenerate cases, and what >>11 said. You're a moron, >>1, and so is >>16.

Name: Anonymous 2010-09-25 21:36

Never use #defines when you can use const enums. Otherwise, you get atrocities like the Bourne Shell source code which has #defined everything so it looks like pseudo-Pascall code.

Name: Anonymous 2010-09-25 21:47

Macros considered harmful unless it's DEBUG.

Name: Anonymous 2010-09-26 6:32

>>17
no u

Name: Anonymous 2010-09-26 7:15

Macross considered AWESOME!

Name: Anonymous 2010-09-26 9:28

What about const vs #define?

Name: Anonymous 2010-09-26 9:52

>>8
enum dicks {
    cocks = 1,
    penises = 1 << 1,
    twigs = 1 << 2,
    swords = 1 << 3,
    meats = 1 << 4
};

Name: Anonymous 2010-09-26 12:11

#define doesnt stick with the standard rules of C++, for pure C i guess theres nothing really wrong with it but if anyone were to use that code in C++ it would mess up in terms of namespaces and locational code...

hence enum is better

Name: Anonymous 2010-09-26 12:56

>>24
back to /pr/, please.

Name: Anonymous 2010-09-26 13:03

>>25
whats /pr/?

Name: Anonymous 2010-09-26 14:07

>>25
But it's not junior high anymore
And don't like to take your shit anymore
I've got a new boyfriend now
And he has got a motorcycle

Name: Anonymous 2010-09-27 0:00

>>24
"...it would mess up in terms of namespaces and locational code."

Locational code?

You mean, it would screw up your GIS/GPS/spatial indexing stuff?

Name: Anonymous 2010-09-27 0:02

>>24
I don't know what languages you know (C++, apparently?) but I suggest that you learn English next.

Name: Anonymous 2010-09-27 17:56

>>28
What he's trying to say is that it would be harder to LOCATE MY ANUS

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List