Do's and Dont's using Switch Case in C

Do's and Dont's using Switch Case in C

1. If we don't use "break" keyword in each case, the control goes to next case.

2. We can write case statements in any order. Default case may be first case, last case or even in between other cases.

3. Case expression can be a macro constant.

4. Case expression cannot be a constant variable.

5. Case expression can be a enum constant.

6. Case expression can be a character constant.

7. Case expression cannot be a string constant.

8. Duplicate case not possible in switch case.

9. Case expression must be integer constant expression. If it is not an integer then it is automatically type casted into a integer value.

10. Macro constants & hexadecimal values are possible.

11. Siwtch expression must return an integer value. It cannot be a float, double or a long double.

12. A character in switch is forced to ASCII value , if the cases are in decimals and if it is not in the range of 1 to 256, then the rotation / cyclic value is used.

13. Switch without cases is meaningless. It will do nothing.

14. Continue statement cannot be used in switch case, unless it is given inside a looping statement inside switch case.

15. Continue is even not allowed in default case.

16. Nesting of switch case statements is possible in C.

17. Possible to write label of goto statement in the case of switch statement.

18. Cannot declare variables in cases.

19. Switch case / sizeof() doesn't affect the value of any variable.