FatalError FatalError - 2 months ago 9
C Question

Why #define values inside an enum?

I ran across something that's odd to me while reading one of the Linux headers,

<linux/rtnetlink.h>
:

/* RTnetlink multicast groups */
enum rtnetlink_groups {
RTNLGRP_NONE,
#define RTNLGRP_NONE RTNLGRP_NONE
RTNLGRP_LINK,
#define RTNLGRP_LINK RTNLGRP_LINK
RTNLGRP_NOTIFY,
#define RTNLGRP_NOTIFY RTNLGRP_NOTIFY
RTNLGRP_NEIGH,
#define RTNLGRP_NEIGH RTNLGRP_NEIGH
RTNLGRP_TC,
#define RTNLGRP_TC RTNLGRP_TC
RTNLGRP_IPV4_IFADDR,
#define RTNLGRP_IPV4_IFADDR RTNLGRP_IPV4_IFADDR
/* ... etc, pattern continues ... */
};


I'm having a hard time understanding the reason for the macros here or what difference they would make. What purpose does this serve?

Answer

It makes the constants visible to the preprocessor. You could use #ifdef to check if a particular constant is available.

Comments