473,326 Members | 2,111 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,326 software developers and data experts.

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 5759
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nospam | last post by:
Hi, I have an application whose textareas rows automatically resize onFocus to the number of lines in the textArea. The cols are set to 100% (in a CSS file) in order to always take the full...
5
by: Claire | last post by:
Is there an easy way to return the number of elements in an enumerated type please thanks Claire
2
by: Daniel Wilson | last post by:
I have an enumerated type like this: public __value enum MyType{ ABC = 1, DEF = 2, GHI = 3, JKL = 4 }; Later I have a for loop like this: for (ft = MyType::ABC; ft <= MyType::JKL; ft++){
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: davidmcb | last post by:
I have defined an enumerated type that I use in various places in my schema. I am in the process of creating a new complex type that contains an attribute that I want to restrict to NOT being a...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
7
by: John Goche | last post by:
Hello, The following program compiler and runs fine under gcc and produces the output 3. However, I am not sure whether such behavior is legal. In particular, as a related question, how would I...
1
by: senfo | last post by:
I'm using an enumerated type to identify a record type that relates to a unique ID in a database. Identity columns in SQL start at 1, while enumerated types in C# start at 0. I didn't think it...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.