Printing values from Enum

This is a discussion on Printing values from Enum within the C Programming forums, part of the General Programming Boards category; I've been struggling with this for some time now. I searched the previous posts and I couldn't find an answer. ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    16

    Printing values from Enum

    I've been struggling with this for some time now. I searched the previous posts and I couldn't find an answer.

    Consider the following.

    Code:
    enum meat {beef, pork, chicken, salmon };
    Now

    Code:
    printf("%d", beef);
    Will print 0. Is there anyway to use the value 0 to print "beef"?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Not As Far As I Know. Great name tho, I hope you don't kill anyone with an axe
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    AFAIK I don't think so since those symbolic names don't exist at runtime.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    you need to maintain the list of strings yourself

    Code:
    const char* meetstr[]={"beef","pork", "chicken", "salmon"};
    printf(%s",meetstr[beef]);
    or some a little more advanced mapping if enums are not always start with 0 and continue flat
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely that should be "meatstr"?

    And as stated, the symbolic names do not exist in the binary unless you specifically produce code to translate the enum to strings.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    Quote Originally Posted by matsp View Post
    Surely that should be "meatstr"?
    I do not know, I'm vegetarian
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Is there anyway to use the value 0 to print "beef"?
    Not in this case, but you may switch to string arrays to obtain the desired behaviour.

    If you need to start at a different value than 0, add an offset. If the integer values don't increase by 1, build your own structure for storing the stuff:

    Code:
    typedef struct {
            int index;
            char buf[1024];
    } kv;
    
    kv stuff[1024];
    
    // ...
    
    stuff[0].index = 17;
    strcpy(stuff[0].buf, "snafu");
    Greets,
    Philip

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    Thanks for the replies. The exercises from the enum/typdef chapter in my C book asked for a lot of enum value printing. Consequently, this chapter is before the array chapter so it took a lot of switch statements.

    Oh and thanks MK27. I'm picking up C in jail, Sonia says its good for me.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Raskalnikov View Post
    Oh and thanks MK27. I'm picking up C in jail, Sonia says its good for me.
    Don't catch consumption!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Controlling variable values by printing on file
    By p3rry in forum C Programming
    Replies: 8
    Last Post: 12-17-2008, 09:09 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. Printing enum field symbolically
    By pdc in forum C Programming
    Replies: 3
    Last Post: 07-05-2005, 12:51 AM
  5. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21