3 ms·
While dealing with unhandled enumeration constants you can get help from most compilers; for gcc: #include <stdio.h> typedef enum tagCmd { cmd1, cmd2, cm
by MichaelMoser123 10y ago
While dealing with unhandled enumeration constants you can get help from most compilers; for gcc:
#include <stdio.h>
typedef enum tagCmd { cmd1, cmd2, cmd3 } Cmd;
void doCmd(Cmd cmd )
{
switch(cmd)
{
case cmd1:
printf("Cmd1\n");
break;
case cmd2:
printf("Cmd2\n");
break;
//case cmd3:
// printf("Cmd3\n");
// break;
}
}
int main()
{
doCmd(cmd1);
return 0;
}
Now
gcc -Wall t.c
t.c: In function ‘doCmd’:
t.c:7:2: warning: enumeration value ‘cmd3’ not handled in
switch [-Wswitch]
switch(cmd)
^
Now
gcc -Wall -Werror t.c
t.c: In function ‘doCmd’:
t.c:7:2: error: enumeration value ‘cmd3’ not handled in
switch [-Werror=switch]
switch(cmd)
^
Or rather the following: (if you don't want to deal with a lot of other errors/warnings)
gcc -Wswitch -Werror t.c
t.c: In function ‘doCmd’:
t.c:7:2: error: enumeration value ‘cmd3’ not handled in
switch [-Werror=switch]
switch(cmd)
^
If you put in a default statement then the compiler will not be able to help you here.