enum and define
1
Name:
Anonymous
2010-09-25 12:15
So tell me, /prog/rammers
In C, why use enum instead of just #define?
2
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
3
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};
4
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
5
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
6
Name:
Anonymous
2010-09-25 12:26
>>4
#define FALSE 0
#define TRUE !FALSE
7
Name:
Anonymous
2010-09-25 12:26
debug symbols
8
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
?
9
Name:
Anonymous
2010-09-25 12:31
>>8
Its called the + operator.
10
Name:
Anonymous
2010-09-25 12:39
>>8 Dont ask too much from C. You want higher level features? Use a higher level language.
11
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][~]%
12
Name:
Anonymous
2010-09-25 18:48
>>11
[0][~]%
what's that?
13
Name:
Anonymous
2010-09-25 18:56
>>12
A zero in brackets, a tilde in brackets and a percent sign.
14
Name:
Anonymous
2010-09-25 19:33
>>12
Usually it's: [return code][pwd]prompt
15
Name:
Anonymous
2010-09-25 19:50
Because enumeration constants do the same job (for int constants) and are far less error prone.
16
Name:
Anonymous
2010-09-25 20:10
The only major advantage I have noticed is when using code-completion (eg in Visual Studio)
17
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 .
18
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.
19
Name:
Anonymous
2010-09-25 21:47
Macros considered harmful unless it's DEBUG.
20
Name:
Anonymous
2010-09-26 6:32
21
Name:
Anonymous
2010-09-26 7:15
Macross considered AWESOME!
22
Name:
Anonymous
2010-09-26 9:28
What about const vs #define?
23
Name:
Anonymous
2010-09-26 9:52
>>8
enum dicks {
cocks = 1,
penises = 1 << 1,
twigs = 1 << 2,
swords = 1 << 3,
meats = 1 << 4
};
24
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
25
Name:
Anonymous
2010-09-26 12:56
>>24
back to /pr/, please.
26
Name:
Anonymous
2010-09-26 13:03
27
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
28
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?
29
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.
30
Name:
Anonymous
2010-09-27 17:56
>>28
What he's trying to say is that it would be harder to
LOCATE MY ANUS