Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-02-2012, 07:19 AM   #1
LQ Newbie
Registered: Aug 2012
Distribution: OpenSUSE 12.1
Posts: 9

Rep: Reputation: Disabled
#defines inside an enum

I was looking at linux/include/linux/rtnetlink.h

enum {
	RTM_BASE	= 16,

Why are these #defines embedded within the enum here? What's the point of this type of code?
Note I'm not talking about this rtnetlink.h functionality specifically... just in general, the only place I've seen #defines embedded in enum's is in linux code.

Last edited by wcrandthor; 10-02-2012 at 09:49 AM. Reason: clarification
Old 10-03-2012, 03:54 PM   #2
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Those #defines seem to be intended to sort of undefine those symbols as preprocessor symbols right after potentially using the preprocessor symbols to define names in the enum.

Out of context, I'm failing to guess the intent.

The preprocessor acts as if it were executed before the rest of compilation, so the preprocessor does not care that the #define happens to land inside the body of the enum definition.

Edit: After using google, I have a better guess (mainly expanding on the answer that you seem to have gotten when asking the same question in a different forum):

Assuming there are many variants of rtnetlink.h floating around (for different OS's etc.) and some of those defined RTM_BASE by #define and some didn't define it at all, then user code depending on rtnetlink.h but wanting to be portable would include code such as
#ifdef RTM_BASE
to see if RTM_BASE is defined. But then a version, such as you quoted, that defines it by enum would break such user code, so redundantly define it by #define (as itself) to allow that test.

Last edited by johnsfine; 10-03-2012 at 04:02 PM.
1 members found this post helpful.
Old 10-03-2012, 04:34 PM   #3
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,942

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Preprocesssor does run in a separated process before the compilation, and yes, these defines are meant for #ifdef's.


code, defined, enumerator, preprocessor variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
MAC OS X defines son_t Programming 2 07-24-2007 04:20 AM
Questions about enum. What would be a good use of enum? RHLinuxGUY Programming 5 06-11-2006 01:13 PM
typedef defines which type? kpachopoulos Programming 1 11-10-2005 02:46 AM
override defines alaios Programming 1 09-14-2005 10:40 AM
OS Conditional Defines kevin.cowley Programming 1 09-24-2003 03:54 PM

All times are GMT -5. The time now is 10:59 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration