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;
}
"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.
Correction:
if(scanf("%d", &temp) != 1)
{
/* Error handling */
}
else
{
opcao = temp;
/* Normal handling */
}