0
int main() {
    char *time = (char *)malloc(10240 * sizeof(char));
    int i = 0;
    scanf("%s", time);
    i = atoi(time);
    if (time[8] == 'P')
        i += (i != 12) ? 12 : 0;
    else
        i += (i != 12) ? 0 : -12; 
    printf("%02d%.6s\n", i, time + 2);
    return 0;
}

I'm new to C programming language! I got this code from HackerRank! This is code for convert 12Hour time to 24Hour time.

Can anyone please explain this part: i += (i != 12) ? 0 : -12

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    It reds like: `i = i + (if i is not 12 then 0, else -12)` – Paul Ogilvie Feb 05 '17 at 08:59
  • 1
    No matter how large the buffer is allocated, `scanf("%s", time);` can overflow this buffer with appropriately coined input. Use `char time[16]; scanf("%15s", time);`. The time string should be 10 byte long anyway. – chqrlie Feb 05 '17 at 09:58

0 Answers0