[C/C++][Tip] Using long and important ‘enum’..

This is tip to define ‘enum’ that has quite lots of items and important information – ex thread id, event id, etc…


// File : "main"


This is not recommended.
How about using following way?


// File : "enum.h "



// File : "main

#define DEF_ENUM(x)   x,
    #include "enum.h"
#undef DEF_ENUM

Why this way is better? Let’s assume that you want to print ‘enum’ values.
In (*1) case, only number value can be printed. But in (*2) case, even enum name – string – can be easily printed by following way.

// File : "Enum_str.c"

#define DEF_ENUM(x)   #x,
static const char* enum_str[] =
    #include "enum.h"

printf("%s", enum_str[enum_id]);

It is good, isn’t it?
Except for this, you can find other lots of useful ways by using macro.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s