In this tutorial, we will write a C program to convert a time from 12 hour format to 24 hour format. For example:
Input: 03:30 PM
Output: 15:30
C Program for 12 hour to 24 hour conversion
Let’s write the code for this conversion. The explanation of the code is provided in the code itself using comments as well as at the end of the program.
#include <stdio.h>
#include <string.h>
// This function converts 12-hour format to 24-hour format
void convertTo24HourFormat(char *time12, char *time24) {
int hour, minute;
char period[3]; // Array to hold AM or PM
// It parse the time entered by the user.
// The sscanf function reads formatted input from a string
// %d reads integers (hour and minute)
// %2s reads a string with at most 2 characters (AM or PM)
sscanf(time12, "%d:%d %2s", &hour, &minute, period);
// Convert the hour part based on whether it's AM or PM
if (strcmp(period, "PM") == 0 && hour != 12) {
// If it's PM and the hour is not 12, add 12 to the hour to convert to 24-hour format
hour += 12;
} else if (strcmp(period, "AM") == 0 && hour == 12) {
// If it's AM and the hour is 12, set the hour to 0 (midnight in 24-hour format)
hour = 0;
}
// Format the 24-hour time string
// The sprintf function writes formatted output to a string
sprintf(time24, "%02d:%02d", hour, minute);
}
int main() {
char time12[10]; // Array for input time in 12-hour format
char time24[6]; // Array for output time in 24-hour format
// Prompt the user to enter the time in 12-hour format
printf("Enter time in 12-hour format (HH:MM AM/PM): ");
// Read the time entered by the user
fgets(time12, sizeof(time12), stdin);
// Removes the newline from the input time string, if present
time12[strcspn(time12, "\n")] = '\0';
// Call the function to convert the time to 24-hour format
convertTo24HourFormat(time12, time24);
// Print the converted time in 24-hour format
printf("Time in 24-hour format: %s\n", time24);
return 0;
}
Explanation of Program:
- Function Declaration and Initialization:
convertTo24HourFormat
: Function to convert 12-hour time to 24-hour format.char period[3]
: To hold “AM” or “PM”.
- Parsing the Input Time String:
sscanf(time12, "%d:%d %2s", &hour, &minute, period)
: This line reads the hour, minute, and period (AM/PM) from the input string.
- Conversion Logic:
- If the period is “PM” and the hour is not 12, it adds 12 to the hour.
- If the period is “AM” and the hour is 12, it sets the hour to 0 (midnight).
- Formatting the 24-Hour Time String:
sprintf(time24, "%02d:%02d", hour, minute)
: This line formats the hour and minute into the 24-hour format string.
- Main Function:
- Prompts the user to enter time in 12-hour format along with period AM/PM.
- Uses
fgets
to read the input and removes the newline character. - Calls the function
convertTo24HourFormat
to convert the time from 12 hour format to 24 hour format and then prints the result.
Output:
Output 1:
Enter time in 12-hour format (HH:MM AM/PM): 02:30 PM
Time in 24-hour format: 14:30
Output 2:
Enter time in 12-hour format (HH:MM AM/PM): 12:00 AM
Time in 24-hour format: 00:00
Output 3:
Enter time in 12-hour format (HH:MM AM/PM): 07:45 AM
Time in 24-hour format: 07:45
Leave a Reply