470,636 Members | 1,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Find the number of elements in an enumerated type?

Is there a standard compliant method to access the number of elements in
an enumerated type in C, either from within the preprocessor or the
running program?

The example below compiles and runs but returns 4 twice (the number of
bytes used to store a value in test) when the needed answer is 5 (the
number of enumerated values in atype).

#include <stdlib.h>
#include <stdio.h>
enum atype {ZERO,ONE,TWO,THREE,FOUR};
int main(void){
enum atype test;
(void) fprintf(stdout,"1: %d\n",sizeof(test));
(void) fprintf(stdout,"2: %d\n",sizeof(enum atype));
(void) exit(EXIT_SUCCESS);
}

One could insert

#define TESTN 5

after the enum statement, and use that, but it still requires the
programmer to count the number of elements, as opposed to having the
computer do it.

Maybe I'm assuming too much. Doesn't the C standard say that the enum
statement, without any "=" within the {}, always assigns the enumerated
values as 0,1,2,3,4? Seems like there should be a way to get the
preprocessor or run time to cough up the number of elements.

I did come up with this method, which will work so long as the special
last element is never displaced from it's position at the end of the
list. It is kind of hideous though:

#include <stdlib.h>
#include <stdio.h>
enum atype {ONE,TWO,THREE,FOUR,FIVE, I_AM_ALWAYS_LAST};
int main(void){
enum atype test;
test = I_AM_ALWAYS_LAST ;
(void) fprintf(stdout,"%d\n",test);
(void) exit(EXIT_SUCCESS);
}
Thanks,

David Mathog
Sep 28 '07 #1
11 5398
David Mathog wrote:
Is there a standard compliant method to access the number of elements in
an enumerated type in C, either from within the preprocessor or the
running program?

The example below compiles and runs but returns 4 twice (the number of
bytes used to store a value in test) when the needed answer is 5 (the
number of enumerated values in atype).
No.

If you must know (which is probably rare), add a known last element.
Even that only works if the values are contiguous and start form 0.

--
Ian Collins.
Sep 28 '07 #2
On Sep 29, 8:33 am, David Mathog <mat...@caltech.eduwrote:
Is there a standard compliant method to access the number of elements in
an enumerated type in C, either from within the preprocessor or the
running program?
Not directly, but you could declare enums in the following way, using
some preprocessing magic:

#include <stdlib.h>
#include <stdio.h>
#define ENUM_START(type) enum type {
#define ENUMCONCAT(type) I_AM_ALWAYS_LAST_ ## type
#define ENUM_END(type) ENUMCONCAT(type) };
#define ENUM_SIZE(type) ENUMCONCAT(type)

ENUM_START(atype)
ONE,
TWO,
THREE,
FOUR,
FIVE,
ENUM_END(atype)

int main(void)
{
enum atype atest = ENUM_SIZE(atype);
(void) fprintf(stdout,"%d\n",atest);
(void) exit(EXIT_SUCCESS);

}

Sep 29 '07 #3
On Sep 29, 2:26 pm, boroph...@gmail.com wrote:
On Sep 29, 8:33 am, David Mathog <mat...@caltech.eduwrote:
Is there a standard compliant method to access the number of elements in
an enumerated type in C, either from within the preprocessor or the
running program?

Not directly, but you could declare enums in the following way, using
some preprocessing magic:

#include <stdlib.h>
#include <stdio.h>
#define ENUM_START(type) enum type {
#define ENUMCONCAT(type) I_AM_ALWAYS_LAST_ ## type
#define ENUM_END(type) ENUMCONCAT(type) };
#define ENUM_SIZE(type) ENUMCONCAT(type)

ENUM_START(atype)
ONE,
TWO,
THREE,
FOUR,
FIVE,
ENUM_END(atype)

int main(void)
{
enum atype atest = ENUM_SIZE(atype);
(void) fprintf(stdout,"%d\n",atest);
(void) exit(EXIT_SUCCESS);
Nice. BUT this way we couldn't assign the value in ENUM.
It's difficult to me.

--coffee

Sep 29 '07 #4
In article <11**********************@y42g2000hsy.googlegroups .com>,
<bo*******@gmail.comwrote:
>#define ENUMCONCAT(type) I_AM_ALWAYS_LAST_ ## type
#define ENUM_END(type) ENUMCONCAT(type) };
#define ENUM_SIZE(type) ENUMCONCAT(type)
I have often used the manual version of this - inserting an extra
item in the enumeration. It has the minor disadvantage that some
compilers (such as gcc) will give a warning about a missing value
in switch statements that cover all the real values.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 29 '07 #5
David Mathog wrote:
Is there a standard compliant method to access the number of elements in
an enumerated type in C, either from within the preprocessor or the
running program?

The example below compiles and runs but returns 4 twice (the number of
bytes used to store a value in test) when the needed answer is 5 (the
number of enumerated values in atype).

#include <stdlib.h>
#include <stdio.h>
enum atype {ZERO,ONE,TWO,THREE,FOUR};
int main(void){
enum atype test;
(void) fprintf(stdout,"1: %d\n",sizeof(test));
(void) fprintf(stdout,"2: %d\n",sizeof(enum atype));
(void) exit(EXIT_SUCCESS);
}

One could insert

#define TESTN 5

after the enum statement, and use that, but it still requires the
programmer to count the number of elements, as opposed to having the
computer do it.

Maybe I'm assuming too much. Doesn't the C standard say that the enum
statement, without any "=" within the {}, always assigns the enumerated
values as 0,1,2,3,4? Seems like there should be a way to get the
preprocessor or run time to cough up the number of elements.

I did come up with this method, which will work so long as the special
last element is never displaced from it's position at the end of the
list. It is kind of hideous though:

#include <stdlib.h>
#include <stdio.h>
enum atype {ONE,TWO,THREE,FOUR,FIVE, I_AM_ALWAYS_LAST};
int main(void){
enum atype test;
test = I_AM_ALWAYS_LAST ;
(void) fprintf(stdout,"%d\n",test);
(void) exit(EXIT_SUCCESS);
}
I use your method, David:
enum eModulation {
EM_FM, EM_AM, EM_CM, EM_NUM_MODS};
--
Thad
Sep 29 '07 #6
Richard Tobin wrote:
<bo*******@gmail.comwrote:
>#define ENUMCONCAT(type) I_AM_ALWAYS_LAST_ ## type
#define ENUM_END(type) ENUMCONCAT(type) };
#define ENUM_SIZE(type) ENUMCONCAT(type)

I have often used the manual version of this - inserting an extra
item in the enumeration. It has the minor disadvantage that some
compilers (such as gcc) will give a warning about a missing value
in switch statements that cover all the real values.
So simply write a suitable switch statement:

switch (enumval) {
case ONE: ...
...
case FOUR: ...
default: fputs("Invalid enumval encountered\n", stderr;
}

or whatever error catching mechanism you prefer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 29 '07 #7
In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>I have often used the manual version of this - inserting an extra
item in the enumeration. It has the minor disadvantage that some
compilers (such as gcc) will give a warning about a missing value
in switch statements that cover all the real values.
>So simply write a suitable switch statement:
>default: fputs("Invalid enumval encountered\n", stderr;
Yes, of course you can do that. But (a) it's annoying to have to do
it in dozens of places, (b) it's putting in code to handle a case that
should never happen, just to shut the compiler up, and (c) it
suppresses the warning when you really have made an error.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 29 '07 #8
Richard Tobin wrote:
CBFalconer <cb********@maineline.netwrote:
>>I have often used the manual version of this - inserting an extra
item in the enumeration. It has the minor disadvantage that some
compilers (such as gcc) will give a warning about a missing value
in switch statements that cover all the real values.
>So simply write a suitable switch statement:
>default: fputs("Invalid enumval encountered\n", stderr;

Yes, of course you can do that. But (a) it's annoying to have to do
it in dozens of places, (b) it's putting in code to handle a case that
should never happen, just to shut the compiler up, and (c) it
suppresses the warning when you really have made an error.
Please do not strip attributions for material you quote.

No, it's not useless. enum objects can hold any value, they are
not limited to the actually specified set. If one gets into your
system you want to know about it soonest. Without the default the
value is just ignored, which may not be a viable resolution.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 30 '07 #9
On Sep 29, 7:47 pm, *coffee* <coffee....@gmail.comwrote:
On Sep 29, 2:26 pm, boroph...@gmail.com wrote:
On Sep 29, 8:33 am, David Mathog <mat...@caltech.eduwrote:
Is there a standard compliant method to access the number of elements in
an enumerated type in C, either from within the preprocessor or the
running program?
Not directly, but you could declare enums in the following way, using
some preprocessing magic:
#include <stdlib.h>
#include <stdio.h>
#define ENUM_START(type) enum type {
#define ENUMCONCAT(type) I_AM_ALWAYS_LAST_ ## type
#define ENUM_END(type) ENUMCONCAT(type) };
#define ENUM_SIZE(type) ENUMCONCAT(type)
ENUM_START(atype)
ONE,
TWO,
THREE,
FOUR,
FIVE,
ENUM_END(atype)
int main(void)
{
enum atype atest = ENUM_SIZE(atype);
(void) fprintf(stdout,"%d\n",atest);
(void) exit(EXIT_SUCCESS);

Nice. BUT this way we couldn't assign the value in ENUM.
It's difficult to me.

--coffee
Sorry, yes I should have stated that you can only use this if the
constants are contiguous and begin with 0, but I figured this would be
clear from Ian's post.

Regards,
B.

Sep 30 '07 #10
In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>>>I have often used the manual version of this - inserting an extra
item in the enumeration. It has the minor disadvantage that some
compilers (such as gcc) will give a warning about a missing value
in switch statements that cover all the real values.
>>So simply write a suitable switch statement:
>>default: fputs("Invalid enumval encountered\n", stderr;
>Yes, of course you can do that. But (a) it's annoying to have to do
it in dozens of places, (b) it's putting in code to handle a case that
should never happen, just to shut the compiler up, and (c) it
suppresses the warning when you really have made an error.
>No, it's not useless.
I didn't say anything about it being useless to check values.
>enum objects can hold any value, they are
not limited to the actually specified set. If one gets into your
system you want to know about it soonest. Without the default the
value is just ignored, which may not be a viable resolution.
Of course. But I don't want to put in code to check it all over the
place, any more than I constantly check my pointers in case they have
suddenly taken on bogus values.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 30 '07 #11
David Mathog wrote:
>Is there a standard compliant method to access the number of elements in
an enumerated type in C, either from within the preprocessor or the
running program?
Not counting the alreaddy discussed special case of all enum values
being contiguous (not necessarily starting at zero,) the only way I
see is by duplicating the list thus:

enum atype {ZERO,ONE,TWO,THREE,FOUR};
enum atype all_atypes[] = {ZERO,ONE,TWO,THREE,FOUR};
#define NUM_ATYPE (sizeof(all_atypes)/sizeof(enum atype))

This would work for any values, (for example, enum atype
{ZERO=0x01,ONE=0x02,TWO=0x04,THREE=0x08,FOUR=0x10} ; )
but I wouldn't like to do this if the number of values declared in a
single enum is large.
--
Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Sep 30 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Daniel Wilson | last post: by
7 posts views Thread by John Goche | last post: by
1 post views Thread by senfo | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.