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 9290109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:20:22+00:00 2026-06-18T20:20:22+00:00

I am referring to a code sample from the opensource project tig . Which

  • 0

I am referring to a code sample from the opensource project tig. Which is a great tool!

file:tig.c

I am struggling to find a reason for defining the request enumeration as follows:

enum request {
#define REQ_GROUP(help)
#define REQ_(req, help) REQ_##req

        /* Offset all requests to avoid conflicts with ncurses getch values. */
        REQ_UNKNOWN = KEY_MAX + 1,
        REQ_OFFSET,
        REQ_INFO,

        /* Internal requests. */
        REQ_JUMP_COMMIT,

#undef  REQ_GROUP
#undef  REQ_
};

even structures as well..

static const struct request_info req_info[] = {
#define REQ_GROUP(help) { 0, NULL, 0, (help) },
#define REQ_(req, help) { REQ_##req, (#req), STRING_SIZE(#req), (help) }
        REQ_INFO
#undef  REQ_GROUP
#undef  REQ_
};

as can be seen REQ_GROUP has been #defined multiple times creating confusion.. atleast to me. Well i know there might be a good reason to do as such.. what is the actual reason to hide the enum/struct definition in the code using macros?

  • 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-18T20:20:23+00:00Added an answer on June 18, 2026 at 8:20 pm

    This is usually down when different treatment should be used on the same data source.

    For example, you might do:

    #define MY_LIST X(Elem1) X(Elem2) X(Elem3)
    

    And then:

    enum MyEnum {
    # define X(e) e,
    
      MY_LIST
    
      Last
    
    # undef X
    };
    

    In which case, MY_LIST is expanded using the current definition of X.

    Now, in the same file, I can also use MY_LIST to create a to_string method

    char const* to_string(MyEnum e) {
        switch(e) {
    #       define X(e) case e: return #e;
    
            MY_LIST
    
            case Last: return "Last";
    
    #       undef X
        }
        return 0;
    } // to_string
    

    This way, the set of values of the enum is only ever written once, and automatically both the enum and a number of methods dealing with it are kept in sync with this set.

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

Sidebar

Related Questions

I'm referring following code to download a file from particular URL. public class FileFromServerExample
Given this JavaScript code (which is just a comment referring to a url): //
I am referring to this project by Jimmy Bogard: http://www.codeplex.com/AutoMapper The code repository site
I am going through some sample code from Apple. The following 5 statements are
I'm referring to the syntax for writing strings in code, including multiline strings and
I get this problem when referring to OpenFEC (openfec.org)-functions in my code: anders@ubuntu:~/workspace/fectest$ gcc
Referring back to this SO post If there is a Grouping category Category which,
Members, scholars, code gurus. My background is far from any computer programming thus my
By referring to this link I try to implement this powerful tool and facing
The syntax checker I am referring to is a neat little tool that I've

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.