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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:12:08+00:00 2026-05-27T20:12:08+00:00

Possible Duplicate: Easy way to use variables of enum types as string in C?

  • 0

Possible Duplicate:
Easy way to use variables of enum types as string in C?

Is there any elegant way to convert a user input string to an ENUM value is straight C, besides the manual way.

A simplified example of calling a function that takes an ENUM as an argument:

enum = {MONDAY,TUESDAY,WEDNESDAY};

...

//Get user to enter a day of the week from command line
...

//Set the work day according to user input
if (strcmp(user_input,"MONDAY")==0){
   SET_WORK_DAY(MONDAY);
} else if (strcmp(user_input,"TUESDAY")==0){
  SET_WORK_DAY(TUESDAY);
}
...

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-27T20:12:09+00:00Added an answer on May 27, 2026 at 8:12 pm
    $ cat wd.c
    #include <stdio.h>
    
    #define mklist(f) \
        f(MONDAY) f(TUESDAY) f(WEDNESDAY)
    
    #define f_enum(x) x,
    #define f_arr(x) {x, #x},
    
    enum weekdays { mklist(f_enum) WD_NUM };
    
    struct { enum weekdays wd; char * str; } wdarr[] = { mklist(f_arr) };
    
    int main(int argc, char* argv[]) {
        int i;
        for (i=0; i < sizeof(wdarr)/sizeof(wdarr[0]); i++) {
            if (strcmp(argv[1], wdarr[i].str) == 0) {
                printf("%d %s\n", wdarr[i].wd, wdarr[i].str);
                return 0;
            }
        }
        printf("not found\n");
        return 1;
    }
    $ make wd
    cc     wd.c   -o wd
    $ ./wd MONDAY
    0 MONDAY
    $ ./wd TUESDAY
    1 TUESDAY
    $ ./wd FOODAY
    not found
    

    is my favorite way to do such things. This ensures that no consistency errors can occur between the enum and the mapping array.

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

Sidebar

Related Questions

Possible Duplicate: Is there an easy way to convert String to Inetaddress in Java?
Possible Duplicate: Is there an easy way to convert a number to a word
Possible Duplicate: Convert Date String to DateTime Object in Python Is there an easy
Possible Duplicate: Is there an easy way in .NET to get “st”, “nd”, “rd”
Possible Duplicate: Is there an easy way to change the color of a bullet
Possible Duplicate: best way to clear contents of .NET’s StringBuilder Is there a quick
Possible Duplicate: Easy way of populating Javabeans based on request parameters Hi, I have
Possible Duplicate: Easy way to disable a UITextField? I have a UITextField. I want
Possible Duplicate: What is an easy way to tell if a list of words
Possible Duplicate: Convert a long hex string in to int array with sscanf I

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.