This is the code that i came up with while solving a book problem where i was asked to write a program that gets input in 12 hour format and gives output in 24 hour format.
Valid inputs are e.g 1:11PM, 1:11P, 1:11pm, 1:11p, 1:11 pm, 1:11 p, 1:11 PM, 1:11 P
Here is the code :
#include<stdio.h>
int main (void)
{
int hr=0,min=0; char time;
scanf("%d : %d %c", &hr, &min, &time);
if(time =='p' || time == 'P' || time == 'PM' || time == 'pm' && hr >= 12)
{
hr +=12;
}
printf("The 24 hour format time is = %d : %d", hr, min);
printf("% Test result = c", test);
return 0;
}
In the if condition i wrote :
if(time =='p' || time == 'P' || time == 'PM' || time == 'pm' && hr >= 12)
As far as i know char type is only allowed to store 1 character. So why does this work when i give a input like this 9:11 PM. How did it work for this condition?
time == 'pm' || time == 'PM'
The output was :
The 24 hour format time is =21:11
Edit 1 :
GNU GCC Compiler is used. The code is in a single file not in a project.