If there is no default case in a switch
statement, the behavior can be unpredictable if that case
arises at some point of time, which was not predictable at development stage. It is a good practice
to include a default
case.
switch ( x ){
case 0 : { - - - -}
case 1 : { - - - -}
}
/* What happens if case 2 arises and there is a pointer
* initialization to be made in the cases . In such a case ,
* we can end up with a NULL dereference */
Such a practice can result in a bug like NULL dereference, memory leak as well as other types of serious bugs.
For example we assume that each condition initializes a pointer. But if default
case is
supposed to arise and if we don’t initialize in this case, then there is every possibility of landing up
with a null pointer exception. Hence it is suggested to use a default
case statement, even though it
may be trivial.