C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-15-2009, 04:14 PM   #1
Registered User
 
Join Date: Feb 2009
Posts: 14
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"?
Raskalnikov is offline   Reply With Quote
Old 02-15-2009, 04:22 PM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Not As Far As I Know. Great name tho, I hope you don't kill anyone with an axe
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-15-2009, 04:23 PM   #3
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
AFAIK I don't think so since those symbolic names don't exist at runtime.
itCbitC is offline   Reply With Quote
Old 02-16-2009, 12:16 AM   #4
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
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
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 02-16-2009, 04:33 AM   #5
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 02-16-2009, 07:13 AM   #6
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by matsp View Post
Surely that should be "meatstr"?
I do not know, I'm vegetarian
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 02-16-2009, 08:56 AM   #7
Complete Beginner
 
Join Date: Feb 2009
Posts: 312
Quote:
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
Snafuist is offline   Reply With Quote
Old 02-16-2009, 10:21 PM   #8
Registered User
 
Join Date: Feb 2009
Posts: 14
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.
Raskalnikov is offline   Reply With Quote
Old 02-16-2009, 10:28 PM   #9
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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!
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Tags
enum printf value

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:33 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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