c_programming_language/c_modern_approach/Pointers/exercises/e_5.c

35 lines
947 B
C

#include <stdio.h>
/* Splitting seconds into hh:mm:ss */
void split_time(long total_sec, int *hr, int *min, int *sec) {
int f_h, f_m, f_s;
f_h = total_sec / 3600;
printf("hours: %d\n", f_h);
total_sec = total_sec - f_h * 3600;
f_m = total_sec / 60;
printf("minutes: %d\n", f_m);
total_sec = total_sec - f_m * 60;
printf("seconds: %ld\n", total_sec);
*hr = f_h;
*min = f_m;
*sec = total_sec;
}
void split_time_otimized(long total_sec, int *hr, int *min, int *sec) {
*hr = (int)(total_sec / 3600);
*min = (int)((total_sec % 3600) / 60);
*sec = (int)(total_sec % 60);
}
int main() {
long total_sec = 5123;
int hours, minutes, seconds;
hours = 0;
minutes = 0;
seconds = 0;
printf("BEFORE: %d:%d:%d\n", hours, minutes, seconds);
/* split_time(total_sec, &hours, &minutes, &seconds); */
split_time_otimized(total_sec, &hours, &minutes, &seconds);
printf("AFTER: %d:%d:%d\n", hours, minutes, seconds);
}