Printing enum field symbolically

This is a discussion on Printing enum field symbolically within the C Programming forums, part of the General Programming Boards category; For debugging purposes, is there any way of programatically retrieving the symbolic name of a field in an enum, rather ...

  1. #1
    pdc
    pdc is offline
    Registered User
    Join Date
    Jun 2005
    Posts
    28

    Printing enum field symbolically

    For debugging purposes, is there any way of programatically retrieving the symbolic name of a field in an enum, rather than just printing its numeric value? I'm pretty sure there's no way in ANSI C, but I'm using GCC... does it have any non-standard hacks that might allow this?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Nothing like this exists as a compiler extension to GCC (that I can find). The only thing close is the Function Names as Strings extension.
    You're better off doing it in a portable fashion:
    Code:
    #include <stdio.h>
    
    typedef enum 
    {
        value_1,
        value_2,
        value_3,
    } MyEnum;
    
    /*---------------------------------------------------------------------------*/
    #ifndef NDEBUG
    /*---------------------------------------------------------------------------*/
    
    typedef struct 
    {
        MyEnum val;
        const char *val_name;
    } MyEnum_TxtMap;
    
    static const MyEnum_TxtMap g_MyEnum_TxtMap[] =
    {
        {value_1, "value_1"},
        {value_2, "value_2"},
        {value_3, "value_3"},
    };
    
    const char* MyEnum2Text(MyEnum val)
    {
        const size_t map_sz = sizeof(g_MyEnum_TxtMap) / sizeof(*g_MyEnum_TxtMap);
        size_t n = 0;
        
        for (; n < map_sz; ++n)
        {
            if (g_MyEnum_TxtMap[n].val == val)
                return g_MyEnum_TxtMap[n].val_name;
        }//for
    
        return "!!invalid MyEnum val!!";
    }
    
    /*---------------------------------------------------------------------------*/
    #else /* NDEBUG */
    /*---------------------------------------------------------------------------*/
    
    const char* MyEnum2Text(MyEnum val) 
    {
        return "!!non-debug build!!";
    }
    
    /*---------------------------------------------------------------------------*/
    #endif /* NDEBUG */
    /*---------------------------------------------------------------------------*/
    
    int main()
    {
        MyEnum val = value_2;
    
        printf("val = %s\n", MyEnum2Text(val));
    
        return 0;
    }
    gg
    Last edited by Codeplug; 07-04-2005 at 09:29 AM.

  3. #3
    pdc
    pdc is offline
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    Thanks, yeah, I just went with something similar myself in the end. It was always going to be an opportunistic hack, nothing Dijkstra would approve of

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Well if you're consistent enough about how you declare and name things, you should be able to write say a perl script (or other scripting language of your choice) to parse your source code and generate the lookup tables automatically.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to print enum field literals
    By patiobarbecue in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2009, 08:48 AM
  2. Printing values from Enum
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 02-16-2009, 09:28 PM
  3. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 08:28 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM

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