C

Asked • 06/26/19

Valid, but worthless syntax in switch-case?

Through a little typo, I accidentally found this construct: int main(void) {  char foo = 'c';  switch(foo)  {   printf("Cant Touch This\\n"); // This line is Unreachable   case 'a': printf("A\\n"); break;   case 'b': printf("B\\n"); break;   case 'c': printf("C\\n"); break;   case 'd': printf("D\\n"); break;  }  return 0; }It seems that the `printf` at the top of the `switch` statement is valid, but also completely unreachable.I got a clean compile, without even a warning about unreachable code, but this seems pointless.Should a compiler flag this as unreachable code?Does this serve any purpose at all?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.