Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use enum ?

1,140 views
Skip to first unread message

Maikon Adams

unread,
Nov 26, 2008, 8:07:11 AM11/26/08
to
My doubt is how I use the enumeration ? What is the type when I use a
scanf for example ? The code below shows my doubt:

typedef enum
{
copying,
concatenation,
comparison,
searching,
other
} Lista;

Lista opcao;

printf("Digite a opcao: \n\n 0 - copying \n 1 - concatenation \n 2 -
comparison \n 3 - searching \n 4 - other \n ");
scanf("%d",opcao); // ----------- ? What is the type ?

switch(opcao)
{
case copying:
printf("Copying\n");
break;

case concatenation:
printf("Concatenation\n");
break;

case comparison:
printf("Comparison\n");
break;

case searching:
printf("Searching\n");
break;

case other:
printf("Other\n");
break;

default:
printf("Nenhum \n");
break;
}

James Kuyper

unread,
Nov 26, 2008, 8:52:28 AM11/26/08
to
Maikon Adams wrote:
> My doubt is how I use the enumeration ? What is the type when I use a
> scanf for example ? The code below shows my doubt:

"Each enumerated type shall be compatible with char, a signed integer
type, or an unsigned integer type. The choice of type is
implementation-defined,110) but shall be capable of representing the
values of all the members of the enumeration." (6.7.2.2p4). Since the
actual size of an enumerated type can be different for different
enumerated types, there's no way that there can be a single correct
format code that could be used for all enumerated types.

If you know for certain which standard type a given enumerated type is
compatible with for a particular implementation, you can probably get
away with using the format specifier for that standard type, but this
will not be portable to other implementations.

> typedef enum
> {
> copying,
> concatenation,
> comparison,
> searching,
> other
> } Lista;
>
> Lista opcao;
>
> printf("Digite a opcao: \n\n 0 - copying \n 1 - concatenation \n 2 -
> comparison \n 3 - searching \n 4 - other \n ");

The portable way to do this would be to scan the value into a temporary
int or unsigned int, as appropriate, and then copy the value from the
temporary into the actual enumeration object:

int temp;
scanf("%d", &temp);
opcao = temp;


The rest of your code could have used 'temp' just as well as opcao, so
why not simply declare opcao as 'int', and drop 'temp'? The net result
is that it often doesn't pay to declare an object of an enumerated type.
Enumerators are used frequently; objects of enumerated types are used
a lot less frequently.

jameskuyper

unread,
Nov 26, 2008, 11:17:33 AM11/26/08
to
James Kuyper wrote:
...

> The portable way to do this would be to scan the value into a temporary
> int or unsigned int, as appropriate, and then copy the value from the
> temporary into the actual enumeration object:
>
> int temp;
> scanf("%d", &temp);
> opcao = temp;

Correction:

if(scanf("%d", &temp) != 1)
{
/* Error handling */
}
else
{
opcao = temp;
/* Normal handling */
}

0 new messages