![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
C# ENUMs listing in MPLAB- Why name ENUMs? |
Posted: Thu Nov 24, 2011 6:14 am |
|
|
Is it possible to show-up the ENUMs listings in MPLAB(with CCS C)? I mean like structs?
Another thing: It seems not to be so necessary to name ENUM. It´s ok for create Enum type variables, but it seems to be not necessary.
For example:
Code: | enum {
jose=1, //my father
ricardo, //brother
joao //my friend
} name;
name ii=ricardo; //I was not able to list Enum options here
//I can use also: int ii=ricardo;
switch (ii) {
case jose: //I was not able to list Enum options here, too
delay_ms(1);
break;
case ricardo:
delay_ms(1);
break;
case joao:
delay_ms(1);
break;
} |
--------------------
SO why naming ENUMs?
It would be good if was possible to only listing the options available in enum name(in switch command)
Anybody have ideas? Why C works at this way?
Am I telling fulishness? _________________ Eduardo Guilherme Brandt |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Nov 24, 2011 6:35 am |
|
|
Careful with the syntax of enums! They are tricky.
Code: |
enum My_Enum_Type{
jose=1, //my father
ricardo, //brother
joao //my friend
};
|
defines a type, My_Enum_Type that is an enum, based on int, which in CCS is unsigned int8. All the enum labels are simply integer values in disguise and so in C they all, in one scope, have to be unique names. You can't have "Fred" in two different enum types, the compiler can't tell the difference. In C++ and C# there are scope resolution operators (eg. My_Enum_Type::jose) and strong typing which allow that to be sorted out.
To use it to create a variable of My_Enum_Type write:
Code: |
enum My_Enum_Type name;
|
Then I think your switch should work if you change ii to name.
You can also define My_Enum_Type type and a variable called name at the same time by:
Code: |
enum My_Enum_Type {
jose=1, //my father
ricardo, //brother
joao //my friend
} name;
|
What you have done is create an unnamed enum type and a variable called name of that type. You can't declare more variables of that type later, as you try to do with ii, as you didn't name the type and so can't reference it.
Personally I prefer to typedef the enum type so that I can use it more simply:
Code: |
// Define an enum type
typedef enum My_Enum_Type{
jose=1, //my father
ricardo, //brother
joao //my friend
};
// Declare two variables of that enum type
My_Enum_Type name, ii;
|
I hope I got that right. It *is* confusing...
RF Developer |
|
![](templates/subSilver/images/spacer.gif) |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
enums |
Posted: Thu Nov 24, 2011 7:29 am |
|
|
You´re right, I put name in wrong place. The correct is as below:
Code: | enum name{
jose=1, //my father
ricardo, //brother
joao //my friend
};
name ii=ricardo; //I was not able to list Enum options here
//I can use also: int ii=ricardo;
switch (ii) {
case jose: //I was not able to list Enum options here, too
delay_ms(1);
break;
case ricardo:
delay_ms(1);
break;
case joao:
delay_ms(1);
break;
} |
but my main question is about ENUM list that shows in MPLAB when I reffer to some enum. It´s is very useful. Is it possible to do?
I use it with some frequency in structs.
In C# and C++ development IDEs there is a resource very useful named "inteli-sense". It would be useful if available for MPLAB with CCS C. It seems to be available only with structs. _________________ Eduardo Guilherme Brandt |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Nov 24, 2011 11:01 am |
|
|
I repeat: In C enums are just names for integer values. They are not *really* a different type, they are just using names for integers. This is "weak" typing. So when the debugger sees a value 2, how does it know that in that particular instance it means "jose"?
As I said earlier, C++ and C# are strongly typed. That means, amongst other things that each enum is a different type totally unrelated to int. This means that debuggers for those languages *can* tell the difference between "jose" and 2, and you can't simply assign one to the other, as you can in C.
Its very unlikely that Microchips debugger in MPLAB is going to be able to pick up the type information from CCS's compiler. The CCS debugger is "C aware", probably meaning it can make more sense of this sort of thing becuase the debugger and compiler are fully integrated when running under CCS IDE.
Enums were a late addition to C. I *think* they may have come first in C++ and other languages and then were added into C when it was standardised: i.e. ANSI C. Before then preprocessor defines were the way to do enum-like functionality.
e.g.
Code: |
#define Fred 1
#define Joe 2
#define John 3
|
and these days in C (though NOT in C++ and C#] that's really all that enums are. It means you can do crazy things like:
Code: |
if (John == Fred + Joe)
{
// Do stuff.
}
|
which you cannot do in C++ or C#.
RF Developer |
|
![](templates/subSilver/images/spacer.gif) |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
enums defines |
Posted: Thu Nov 24, 2011 12:07 pm |
|
|
Thank you a lot RF_Developer.
I didn´t know that Enum was a late addition in C.
Anyway, a lot of constant aliases grouped with a enum makes the program more beautiful and clean(better than use a lot of defines).
I´ve used it for defining SPI commands or addresses aliases for communication with another processor.
Thanks again. _________________ Eduardo Guilherme Brandt |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Mon Feb 23, 2015 2:39 am |
|
|
Enum's were a fairly 'early' addition, but were not in C originally. They appeared in the second edition 'The C programming language', but not in the first edition. The section in this book, is worth perhaps quoting:
"Names in different enumerations must be distinct. Values need not be distinct in the same enumeration.
Enumerations provide a convenient way to associate constant values with names, and alternative to #define, with the advantage that the values can be generated for you. Although variables of enum types may be declared, compilers need not check that what you store in such a variable is a valid value for the enumeration Nevertheless enumeration variables offer the chance of checking and so are often better than #defines."
The keywords here are 'chance of checking', so the potential is there to do type/value checking based upon an enum, but as RF_Developer has already pointed out, this is not really possible in the configuration we are dealing with.
What you have found is exactly the advantage they have, and they become even better practice if you move to later languages.
However if you think of them 'internally' as akin to defines, then you can better understand the limitations compared to a genuine 'type'. ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|