Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case with logical operator in C

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.

like image 430
Er Avinash Singh Avatar asked Nov 05 '12 04:11

Er Avinash Singh


1 Answers

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.

like image 176
Jeyaram Avatar answered Nov 09 '22 11:11

Jeyaram