-6
  main(){

  int num = 'b';

  switch(num)
  {
      default: printf("Yes\n");
      case 'a': printf("Why!\n"); break;
      case 'A': printf("Bye"); break;

  }
}

Why the output of this code is : Yes Why!

Why the case 'a' is also executed ?

Kernull
  • 19
  • 5
  • 7
    because there's no `break` after the `default:` case? – EOF Jan 13 '15 at 17:42
  • 2
    It's called [fallthrough](http://en.wikipedia.org/wiki/Switch_statement#Fallthrough). – Cornstalks Jan 13 '15 at 17:43
  • break has a meaning in C switches ... – philippe lhardy Jan 13 '15 at 17:43
  • 1
    ask yourself: why is the 'A' case not executed? – njzk2 Jan 13 '15 at 17:44
  • usualy default is the last case; this look like 'special' code... – philippe lhardy Jan 13 '15 at 17:47
  • shouldn't num be a char? – El Devoper Jan 13 '15 at 17:48
  • 1
    Thanks @Cornstalks that's what i want to know. – Kernull Jan 13 '15 at 17:49
  • @ElDevoper why should `num` be a char? Library functions such as `tolower()` take and return `int`. – Weather Vane Jan 13 '15 at 17:51
  • @WeatherVane, because the value of num ('b') isn't an integer. – El Devoper Jan 13 '15 at 17:58
  • 1
    @ElDevoper yes it is, `'b'` has the integer value 98 which also fits the `char` range.. – Weather Vane Jan 13 '15 at 18:01
  • using default first or at any place works, it is not how it usualy is documented , http://stackoverflow.com/questions/3110088/switch-statement-must-default-be-the-last-case is a good question indeed, that's why i +1. – philippe lhardy Jan 13 '15 at 18:03
  • @WeatherVane it doesn't make sense to write `int num = 'b'` instead of `int num = 98` and after comparing it with an char. Looks like a mistake for me, even if it is valid Code. – El Devoper Jan 13 '15 at 18:20
  • @ElDevoper tell that to the programmers who wrote library functions such a `tolower()` and `strchr()` both of which take `int` parameters for the `char` you think you pass. This loop `for(int i='a'; i<='z'; i++)` is not a mistake which happens to work. It is correct. – Weather Vane Jan 13 '15 at 18:28
  • @WeatherVane as I said before, it is valid code. If you wanna go to the limits of an type safe language: Have Fun^^! But if I want to write `int i = 98` I don't even think about `int i = 'b'`. And I'm sure the programmers that have to rewrite my code will be thankfull for that. http://en.wikipedia.org/wiki/KISS_principle – El Devoper Jan 13 '15 at 18:56
  • @ElDevoper for a moment, I thought your link would be about the use of `'b'`. I would have thought `i='b';` was a lot more *KISS* than `i=98;` in the context of referring to the alphabet, as it shows those following exactly what you were doing. – Weather Vane Jan 13 '15 at 19:11
  • @WeatherVane I just wanted to give a hint in a comment and ended up in an endless discussion. Your rendition of KISS is more like KI_S. Even the only/first answerer of this question seems to understand what I mean! `char str[] = { 115, 109, 97, 114, 116, 97, 115, 115, 33 };` ;) – El Devoper Jan 13 '15 at 19:44
  • @ElDevoper I would be happier if you spelled it correctly and put `83` as the first character ;) – Weather Vane Jan 13 '15 at 19:50

1 Answers1

1

Try This Code: Will Work Fine

int main()
{
      char num = 'b';
      switch(num)
      {          
          case 'a': printf("Why!\n"); break;
          case 'A': printf("Bye"); break;
          default: printf("Yes\n");
      }
  }

Put Default Case on End

OR

Put break after Default

Use char instead of int

int main()
{
      char num = 'b';
      switch(num)
      {   
         default: printf("Yes\n"); break;
          case 'a': printf("Why!\n"); break;
          case 'A': printf("Bye"); break;

      }
  }
Khurram Sharif
  • 504
  • 1
  • 4
  • 20