enums, pcc & all that

Robert L. Fair cim2 at pyuxv.UUCP
Tue Jun 17 13:45:38 AEST 1986


Several folk seemed to have missed the point in my recent posting on
enums - A true enumerated type is a useful thing because it:

*  catches lots of silly bugs with constant values, especially with
   variables representing finite states in large applications.
*  makes the code more readable & maintainable, especially in the 
   definitions section - all the possible value for a datatype are
   gathered togther, and clearly marked:

	enum { RUN, WAIT, SLEEP, READ, WRITE, SLUGS } state;
   or
	#define RUN 1
	#define WAIT 2
	#define SLEEP 3
	#define READ 3
	#define WRITE 5
	#define SLUGS 6
	int	states;
   (yes - there is a bug in the #defines...)

Unfortunatly those that be decided to do a STUPID, DUMB and
and CRETINOUS implementation of enums as integers (from those that
bring you unary+ ...)

As to the comment on printf not being able to print enums, 
it can't print structures or unions either - so why worry!
Remember that enums are usually used to represent states or control flow
in a program - they aren't often printed out (except when debugging etc -
and for that you can always give a regular message:

	enum { ALIVE, RUNNING, COLLAPSED } jogger;
	...
	if(jogger==ALIVE)
	{
#ifdef DEBUG
		printf("hey, he's alive\n");
#endif
	...
	}
or coerce to an int if need be.)
For more general cases, use C++ !

The only compiler I know which does enums right is Microsoft 3.0 for the IBM-PC.

Rob. Fair
Bell Communications Research
ihnp4!pyuxv!cim2



More information about the Comp.lang.c mailing list