-2

I've been weirded out with this example I stumbled upon in my courses of C language, normally it should be incrementing nb_chiffres everytime c happens to be a digit (it's like a loop in which everytime we put c = getchar();) and increment nb_non_chiffres when it's otherwise.

My problem is that cases do not end in ";" nor do they end in "break;" so I see it as if case '1' is included in case '2' and case '3' in '2' etc.. and I don't see the program working as intended. I know if case '0' is not true it will execute all cases below save for default but why didn't my teacher put the ";" at the end of the cases? Here's the code

switch(c){
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        nb_chiffres++;
        break;
    default:
        nb_non_chiffres++;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 4
    Two comments: (1) You shouldn't have leading or trailing whitespace inside your single-quote character constants, e.g. change `' 0 '` to `'0'` and (2) Switch cases flow through to the one that follows, unless explicitly exited via `break`, `return`, `goto`, etc. – Tom Karzes Feb 18 '19 at 20:52
  • Aside, the use of `switch` could replaced with more compact code: `if(isdigit(c)) { nb_chiffres++; } else { nb_non_chiffres++; }` – Weather Vane Feb 18 '19 at 20:56

1 Answers1

0

Edit: When I say this is legal C code, I mean the absence of semicolon and break against case statements. Apart from that there are some formatting issues which will cause compilation errors as mentioned in comments.

This is actually legal C code, and this way of writing a Switch statement in C-like languages even has a name: fallthrough.

The code you've shared is doing two things:

  1. First, the cases have been left empty and their is no statement corresponding to them, so there is no semicolon in front of them.
  2. Second, the absence of break in all those cases will cause control to essentially cycle through all cases up to default unless a break is encountered.

Have a look at this question.

Akashdeep Singh
  • 388
  • 4
  • 14
  • This is not really fall through. In C a statement can have multiple labels, and a case label is a label. Fall through with an empty statement would be `case '0':; case '1': do_something();` (Note the semicolon.) – rici Feb 18 '19 at 21:00
  • Could you please point to any docs that says it's not fall-through? As far as I understand, it's just a special case of a fall-through, where the case doesn't have statements against it. – Akashdeep Singh Feb 18 '19 at 21:09
  • 2
    @AkashdeepSingh: Per C grammar, as shown in C 2018 6.8.1 1, `case 0: case 1:… case 9: nb_chiffres++;` is one combined statement, not ten sequential statements. `case 0:` by itself is just a label; it is not a statement. So a switch to `case 0:` jumps to one statement that has ten labels; it does not jump to the first of ten statements and then fall through to the rest. There are no statements between `case 0:` and `nb_chiffres++;` to go through. – Eric Postpischil Feb 18 '19 at 21:22