BETA Try the new lesson view
Code

The daytime module

Move the time functions into the daytime module. The program behaves exactly the same: only where things are written changes.

In CLion: right-click practice/07 → New → C/C++ Header File (daytime.h) and New → C/C++ Source File (daytime.c). If CLion offers to add the file to a target, check CMakeLists.txt anyway.

daytime.h – the DAYTIME_H guard, the MINUTES_PER_DAY constant and five declarations. The contract comments move here together with the declarations: whoever uses the module reads them.

daytime.c – first #include "daytime.h", then what the bodies need (assert.h, stdio.h, input.h), and the five definitions unchanged.

main.c – only main stays. Take out the five functions at the top, the #define and the #include lines main no longer needs (assert.h, input.h), and add #include "daytime.h".

CMakeLists.txt – daytime.c goes into the program's list of files:

add_executable(p07_duration main.c daytime.c)

main.c is compiled knowing only the declarations, and the linker connects the calls to the definitions in daytime.c.

Run it

p07_duration and ▶. This time try invalid times too: 25 and 0, 9 and 70, then 9 and 0; for the end 8 and 30, then 10 and 30:

Start
Hour: 25
Minutes: 0
There is no such time.
Hour: 9
Minutes: 70
There is no such time.
Hour: 9
Minutes: 0
End
Hour: 8
Minutes: 30
The end must be after the start.
Hour: 10
Minutes: 30
Lecture 09:00-10:30, duration 90 min (1 h 30 min)

Commit: Add daytime module.