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

  • Home
  • SEARCH
  • 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 6760387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:03:31+00:00 2026-05-26T14:03:31+00:00

I am working with a simple command line application that takes in ASCI text

  • 0

I am working with a simple command line application that takes in ASCI text and interprets it as a command.

I have attempted to minimize the redundancy in this application via the example at http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html.

eg:
Consider a C program that interprets named commands. There probably needs to be a table of commands, perhaps an array of structures declared as follows:

 struct command
 {
   char *name;
   void (*function) (void);
 };

 struct command commands[] =
 {
   { "quit", quit_command },
   { "help", help_command },
   ...
 };

It would be cleaner not to have to give each command name twice, once in the string constant and once in the function name. A macro which takes the name of a command as an argument can make this unnecessary. The string constant can be created with stringification, and the function name by concatenating the argument with `_command’. Here is how it is done:

 #define COMMAND(NAME)  { #NAME, NAME ## _command }

 struct command commands[] =
 {
   COMMAND (quit),
   COMMAND (help),
   ...
 };

Now, let’s say that I want to have a command string and index (ie: int) value, rather than a string and function pointer.

 struct command
 {
   char *name;
   int command_idx;
 };

Now, I have a means to name commands, and have some sort of index I can use later on to identify each command programatically. For example, I have a switch statement that operates on the command index. If I want to work on these indexes, I have to manually set the values first.

I can manually create an enumerated data type, but then I have define the enumerated constants in a separate enum statement. IE:
enum commands { cmd_quit = 0, cmd_help }
and in the end, I still end up having to type each command name twice: once via the COMMAND() macro, and again in my enum.

Is there any method using the C preprocessor that would allow me to create a macro creates the “command” struct (with string and int members), and auto-numbers the int value (command_idx) as I add more commands via the COMMAND() macro?

I am also aware that I can just use strcmp() calls on each possible command, and compare to the input provided by the user, but I would like to have a direct means of indexing into commands via the command_idx value, as opposed to strcmp’ing against a massive list of commands each time (ie: O(1) instead of O(n) ). I also want to avoid having to type the command name more than once at all costs.

Thanks!

  • 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-05-26T14:03:31+00:00Added an answer on May 26, 2026 at 2:03 pm

    You can use macro redefinition to achieve this. First, you create a file that simply lists your commands called commands.inc:

    COMMAND(quit)
    COMMAND(help)
    ...
    

    Then, in your C source you can #include "commands.inc" multiple times, with different definitions of COMMAND() in effect to control how it works. For example:

    struct command
    {
       char *name;
       int command_idx;
    };
    
    #define COMMAND(NAME) CMD_ ## NAME,
    
    enum command_enum {
    #include "commands.inc"
    };
    
    #undef COMMAND
    
    #define COMMAND(NAME) { #NAME, CMD_ ## NAME },
    
    struct command commands[] =
    {
    #include "commands.inc"
    };
    
    #undef COMMAND
    

    (Note that this particular example relies on a C99 improvement that allows a trailing , at the end of the lists in the enum declaration and compound initialiser – you can easily work around that in C89 by adding a dummy entry at the end).

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

Sidebar

Related Questions

While working with Akka, I have implemented a simple command line app. But Akka
I have a simulation software (C++) that runs on the command line. It is
I have a command-line utility that gets quite a bit of downloads from my
I'm working on a simple command line project. I changed the properties from .NET
To learn how to use sockets, I've been working on a simple command-line chat
I'm currently working on a small utility program that only requires a command line
I am working on a simple chat application using a System.Windows.Forms.WebBrowser Control to display
I'm working on a simple javascript login for a site, and have come up
I am working on a simple notification service that will be used to deliver
I've got a simple command line Java JAX-WS app to test a SOAP request,

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.