+ 5

Need a little help with switch

This is my program: #include <stdio.h> int main() { int i = 0; switch (i) { case '0': printf("IXEL"); break; case '1': printf("FOR"); break; default: printf("TRAINING"); } return 0; } shouldn't asnwer be IXEL?? answer is TRAINING Why???

24th Jan 2018, 4:28 AM
Ayush walia
Ayush walia - avatar
4 Antworten
+ 4
It’s because of data types. ‘0’ and 0 are not the same thing, ‘0’ means a character. Here is the corrected code: #include <stdio.h> int main() { int i = 0; switch (i) { case 0: printf("IXEL"); break; case 1: printf("FOR"); break; default: printf("TRAINING"); } return 0; }
24th Jan 2018, 4:32 AM
Jacob Pembleton
Jacob Pembleton - avatar
+ 1
'0' is a char data type so to correct it you should write case 0: and case 1:
24th Jan 2018, 4:59 PM
Jamyang Lotus
Jamyang Lotus - avatar