473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to print enums?

13
Hi,

Does enyone know how to print string describing the enum value?
For example:

enum MyEnum{F1=1, F2};
...
MyEnum me = F2;
printf("My Value: %d", me);
...
I get string: "My Value: 2", but how can i format the string to get strong like:
"My Value: F2" ?

Thanks in advance for help?

kenk
Nov 10 '06 #1
3 76259
Banfa
9,065 Expert Mod 8TB
There is no short cut
Expand|Select|Wrap|Line Numbers
  1. if (me == F2)
  2. {
  3.     printf("My Value: F2");
  4. }
  5. else if (me == F2)
  6. {
  7.     printf("My Value: F1");
  8. }
  9. else
  10. {
  11.     printf("My Value: Invalid Enum Value");
  12. }
  13.  
When using enums I often implement a function that returns const char * to convert the enum value to a string of it's name to make it easily accessable for debug messages.
Nov 10 '06 #2
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. if (me == F2)
  2. {
  3.     printf("My Value: F2");
  4. }
  5. else if (me == F2)
  6. {
  7.     printf("My Value: F1");
  8. }
  9. else
  10. {
  11.     printf("My Value: Invalid Enum Value");
  12. }
  13.  
This will work, but if you want to save some typing, use a switch structure:

Expand|Select|Wrap|Line Numbers
  1. switch(me) {
  2.    case F1: printf("My Value: F1"); break;
  3.    case F2: printf("My Value: F2"); break;
  4.    default: printf("My Value: Invalid Enum Value");
  5. }
Nov 10 '06 #3
Banfa
9,065 Expert Mod 8TB
This will work, but if you want to save some typing, use a switch structure:
Quite and in fact particularly true if the enum has >>>2 values
Nov 10 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
4
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
13
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.