Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:25:27+00:00 2026-06-12T16:25:27+00:00

I frequently use enum in C to access array elemnets with numerical data, e.g.

  • 0

I frequently use enum in C to access array elemnets with numerical data, e.g.

#define KEYS_MAX 1
#define FIELD_MAX 2

enum {FIELD1=0, FIELD2};

double array[KEYS_MAX][FIELD_MAX];
array[1][FIELD1] = 1.0; array[1][FIELD2] = 2.0;

I then print the data to a file in KEYS_MAX lines and FIELD_MAX columns. To know later the content of the columns I would like to print a header line.

# KEY FIELD1 FIELD2
1 1.0 2.0

I would like to have a routine which does this during runtime correctly even if I update the code by changing only enum. I.e., how can I print the header line using C-code and possibly macros using a fixed routine independent of updates of enum?

I found this: Mapping enum values to strings in C++ , but I would prefer something which would also work with the intel compiler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:25:28+00:00Added an answer on June 12, 2026 at 4:25 pm

    You could play some preprocessor tricks. As an exmple, in GCC source tree implementation, you might get inspired by gcc/tree.def files.

    So you might have one file, e.g. myenum.def, with e.g. things like

    MYENUM(Pink)
    MYENUM(Red)
    MYENUM(Ivory)
    

    Then, you might define your enum with some code like

    enum my_enum {
    #define MYENUM(Name) Name,
    #include "myenum.def"
    #undef MYENUM
    };
    

    (You might want to put None_ before the #include above, and Last_ after it)

    Then you could have an enum to string converter with e.g.

    const char* myenum_to_string(enum my_enum en) {
      switch(en) {
    #define MYENUM(Name) case Name: return #Name;
    #include "myenum.def"
    #undef MYENUM
      default: return NULL; /* should not happen */
      }
    }
    

    and you might have a string to enum converter with e.g.

    enum myenum string_to_my_enum (const char*str) {
    #define MYENUM(Name) if (!strcmp(str, #Name)) return Name;
    #include "myenum.def"
    #undef MYENUM
      else abort ();
    }
    

    All the above is common practice, not tied to a particular compiler (it should work with gcc, clang, tcc, icc or any C99 compliant compiler).

    In addition, if you have a large code base (than you can compile with a recent gcc) in which you have hundreds of enum and you don’t want to play such tricks for every of them, you could e.g. develop a GCC plugin or extension (in MELT for instance) to generate -using the internal representations inside GCC of your code- the similar C code just from the enum declarations. If you are coding a new software, or you know your software base quite well, you could replace the enum code with similar tricks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I frequently use the $exception pseudovariable to gain access to the items in the
I frequently use Moose to make sure my data have suitable default values, like
I am involved in projects where we frequently use SQL to hit a data
I use temp tables frequently to simplify data loads (easier debugging, cleaner select statements,
I frequently end up storing data in DOM elements and make heavy use of
I frequently use git stash and git stash pop to save and restore changes
I frequently use the following pattern to set an upper bound to the running
Hi I have SVN repo that I frequently use for change management. However after
I have a function that I use quite frequently, which allows me to write
I have an application that makes use of frequently updated lists. So for example,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.