insane
insane

Reputation: 729

Default case in a switch condition

I have this code:

  #include<stdio.h>                                   
  int main()
  {   
      int a=10;
      switch(a)
      {   
      case '1':
          printf("ONE\n");
          break;
      case '2':
          printf("TWO\n");
          break;
      defalut:
          printf("NONE\n");
      }   
      return 0;
  }

The program doesn't print anything, not even NONE. I figured out that default had a typo defalut!
I want to know why this syntax error is not detected by the compiler.

Upvotes: 28

Views: 2518

Answers (3)

Tobas
Tobas

Reputation: 351

tip: if you are using gcc, add the option -pedantic. it will warn you for unused labels:

$ gcc -ansi -Wall -pedantic test.c -o test
test.c: In function ‘main’:
test.c:14:10: warning: label ‘defalut’ defined but not used

Upvotes: 8

Thom Smith
Thom Smith

Reputation: 14086

That's not a syntax error. defalut is a valid label, and it could be the target of a goto.

Upvotes: 11

user142162
user142162

Reputation:

defalut is just a label in your program that you can jump to with goto. Having an editor that highlights keywords could have made this error easier to spot.

I should also note that your program may have some logic errors. The character '1' is not the same as 1, and the same with '2' and 2.

Upvotes: 35

Related Questions