+ 6

Why is the output here "Sololearn"?

#include <stdio.h> int main() { int i=0; switch(i){ case '0': printf("Solo"); break; case '1':printf("Learn"); break; default:printf("Sololearn"); } return 0; }

23rd Jan 2019, 12:56 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
3 Answers
+ 5
There is no switch option for integer 0 so default switch is used.
23rd Jan 2019, 1:29 PM
Hubert Dudek
Hubert Dudek - avatar
+ 1
maybe you didn't notice quotes)) 0 is not equal to '0' and is not equal to '1'
23rd Jan 2019, 1:09 PM
Š˜Š³Š¾Ń€ŃŒ ŠÆŠŗŠ¾Š²ŠµŠ½ŠŗŠ¾
Š˜Š³Š¾Ń€ŃŒ ŠÆŠŗŠ¾Š²ŠµŠ½ŠŗŠ¾ - avatar
+ 1
0 and 1 are characters inside switch. When you compare an integer with character, ASCII value of the character is matched with the character. And ASCII value of character 0 is not equal to 0.
23rd Jan 2019, 1:26 PM
Nikhil Sharma
Nikhil Sharma - avatar