# Thread: Stuck on function exercise

1. ## Stuck on function exercise

I've been learning C on my own from "C How to Program." There is this one practice exercise I'm having trouble wrapping my head around.

It goes. Write a function that takes the time as three integer arguments(for hours, minutes, and seconds), and returns the number of seconds since the last time the clock "struck 12." Use this function to calculate the amount of time in seconds between two times, both of which are within one 12-hour cycle of the clock.

My problem is whenever I input 0, 12, or 24 hours the program doesn't work. I've been at this for a couple days now any help would be greatly appreciated.
Code:
```#include <stdio.h>

long time(int, int, int);

int main()
{
int h1, h2, m1, m2, s2, s1, sentinel = 1;

while (sentinel == 1) {

scanf("%d%d%d", &h1, &m1, &s1);
if (h1 > 24 || h1 < 0 || m1 > 59 || m1 < 0 || s1 > 59 || s1 < 0) {
printf("Time is out of range\n");
continue;
}

scanf("%d%d%d", &h2, &m2, &s2);
if (h2 > 24 || h2 < 0 || m2 > 59 || m2 < 0 || s2 > 59 || s2 < 0) {
printf("Time is out of range\n");
continue;
}
/* ------------ Why isn't the following if statement allowed?---------*/
if ( !( h2 >= 12 && h1 >= 12 ||  h2 <= 12 && h1 <= 12 ) ) {
printf("The two times must be in the same twelve hour cycle\n");
continue;
}

if ( time(h1, m1, s1) > time(h2, m2, s2) ) {
/*  Test statement  */
printf("time1: %d  time2: %d", time(h1, m1, s1), time(h2, m2, s2) );

printf("%d\n", time(h1, m1, s1) - time(h2, m2, s2) );
}
else {
/*  Test Statement  */
printf("time1: %d   time2: %d", time(h1, m1, s1), time(h2, m2, s2) );

printf("%d\n", time(h2, m2, s2) - time(h1, m1, s1) );
}
}

return 0;
}

long time(int hours, int minutes, int seconds)
{
return hours % 12 * 3600 + minutes * 60 + seconds;
}```

2. What do you mean "not allowed"?
When I compile it, I get
Code:
```\$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function `main':
foo.c:24: warning: suggest parentheses around && within ||
foo.c:31: warning: int format, long int arg (arg 2)
foo.c:31: warning: int format, long int arg (arg 3)
foo.c:33: warning: int format, long int arg (arg 2)
foo.c:37: warning: int format, long int arg (arg 2)
foo.c:37: warning: int format, long int arg (arg 3)
foo.c:39: warning: int format, long int arg (arg 2)```
All the "int format" warnings are telling you that your "%d" should be "%ld" to match the types of the values you're printing.

The "suggest parentheses" is telling you that programmers often mis-understand the precedence of these operators, and that using parentheses will clear it up
if ( !( (h2 >= 12 && h1 >= 12) || (h2 <= 12 && h1 <= 12) ) )

3. Code:
```long time(int hours, int minutes, int seconds)
{
return hours &#37; 12 * 3600 + minutes * 60 + seconds;
}```
Although you're not using time.h, this function name conflicts with that of the standard C lib time() implementation. I would suggest changing it to avoid confusion and to future-proof your code.