Valid, but worthless syntax in switch-case?

后端 未结 8 1433
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 03:38

Through a little typo, I accidentally found this construct:

int main(void) {
    char foo = \'c\';

    switch(foo)
    {
        printf(\"Cant Touch This\\n\");         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 04:24

    It is possible to implement a "loop and a half" with it, although it might not be the best way to do it:

    char password[100];
    switch(0) do
    {
      printf("Invalid password, try again.\n");
    default:
      read_password(password, sizeof(password));
    } while (!is_valid_password(password));
    

提交回复
热议问题