We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
what is the use of default in switch-case?
It is similar to tht 'else' in an 'if' statement. Something to do if no specific alternative matches.
Don't you have a good C book to read? If not: Do get one!
to catch coding mistakes with few exceptions, if default: get entered you have made a mistake
Erik
Entering the default clause in a state machine (where the switch variable is normally an enumerator) should preferably be coded to represent an error.
But a switch statement is used for way more than just state machines.
When decoding charactesr in a parser, you may have:
switch (ch) { case '(': do_lparen(); break; case ')': do_rparen(); break; case '+': do_add(); break; ... default: if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') { do_alpha(ch); } else if (ch >= '0' && ch <= '9') { do_digit(ch); } else { invalid_character(ch); } }