Switch case with logical operator in C

Er Avinash Singh picture Er Avinash Singh · Nov 5, 2012 · Viewed 37.7k times · Source

I am new to C and need help. My code is the following.

 #include<stdio.h>  
 #include<conio.h>  
 void main()
 {

  int suite=2;  

  switch(suite)
     {           
      case 1||2:
      printf("hi");

      case 3:
      printf("byee");

      default:
      printf("hello");
     }

  printf("I thought somebody");
  getche();
  }

I am working in Turbo C and the output is helloI thought somebody. There's no error message.

Please, let me know how this is working.

Answer

Jeyaram picture Jeyaram · Nov 5, 2012
case 1||2:

Becomes true. so it becomes case 1: but the passed value is 2. so default case executed. After that your printf("I thought somebody"); executed.