Header files with enums instead of defines?

Garance A Drosihn drosih at rpi.edu
Tue Dec 21 18:06:36 PST 2004


At 11:31 AM +1030 12/22/04, Greg 'groggy' Lehey wrote:
>
>To find out what that means, I need to go to
>/usr/src/include/sys/errno.h and look for 17.  I find:
>
>#define	EEXIST		17		/* File exists */
>
>If we were to change this to
>
>enum EEXIST = 17;	                /* File exists */
>
>I'd then be able to see:
>
>   xerrno = EEXIST,
>
>That makes debugging a whole lot easier.  About the only down side
>I can see is that you can't #undef an enum.  Is this a big deal?

You also can't #ifdef an enum.

You also can't:
     enum EEXIST = 17;
I'm not a C expert, but you need something more like:
     enum ERRVALS { EEXIST = 17 };
At least that compiles for me.

Some C compilers (irix) will also complain if you assign an
enum-value (such as EEXIST) to a variable which isn't of the same
enum-type (eg: ERRVALS).  So, in those compilers you would have
to define 'xerror' as 'enum ERRVALS xerror', not 'int xerror'.
I think that isn't part of the C standard, but on occassion that
check has found a few bugs for me...

-- 
Garance Alistair Drosehn            =   gad at gilead.netel.rpi.edu
Senior Systems Programmer           or  gad at freebsd.org
Rensselaer Polytechnic Institute    or  drosih at rpi.edu


More information about the freebsd-arch mailing list