0

Made a digital clock using C program : This is a simple digital clock i made and the issue i am having is that every time the time gets incremented the printf gets printed . Since i am on mac i cant use <conio.h> .

My expectation : I want the program to display a single printf and the increment happens in a single printf instead of new printf every time the time changes.

#include<stdio.h>
#include <unistd.h>
#include <stdlib.h >
int main()
{
    int h, m , ;
    int d=1;
    printf("Enter the time : ");
    scanf("%d%d%d", &h,&m,&s);

    if(h>12 || m>60 || s>60){
        printf("ERROR!!");
        exit(0);
    }
    while(1){
        s++;
        if(s>59){
            m++;
            s=0;
        }
        if(m>59){
            h++;
            m=0;
        }
        if(h>12){
            h=1;
        }
       printf("\n Clock ");
        printf(" %02d:%02d:%02d",h, m ,s);
        sleep(1);
    } 
    return 0;
}   
Som
  • 61
  • 1
  • 6
  • 2
    If I understand your question correctly, you want it to display on a single line and not print on a new line every time? If so, a trivial alternative is to use the carriage return character, `\r`, to go back to the start of the line and the next print-out will replace the old line as long as the new output is at least as long as what was there before. It's not universally supported, but you could possibly also play around with [escape codes](https://en.m.wikipedia.org/wiki/ANSI_escape_code) – frippe Mar 20 '22 at 15:49
  • 1
    You might also need to `fflush(stdout)` after each update. Note that the 1-second sleep interval won't guarantee that your clock keeps good time. – Weather Vane Mar 20 '22 at 15:51

2 Answers2

0

You need more thorough error checking on the time entered (what if the user enters negative values, or minutes or seconds equal to 60); and to get the time to print on a single line, you need to replace the \n with a \r, and to flush stdout.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
    unsigned h, m, s;

    printf("Enter the time : ");
    scanf("%u:%u:%u", &h,&m,&s);

    if(h > 12  ||  h < 1  ||  m > 59  ||  m < 0  ||  s > 59  ||  s < 0) {
        printf("ERROR!!");
        exit(0);
    }
    while (1) {
        if (++s > 59) {
            s = 0;
            if (++m > 59) {
                m = 0;
                if(++h > 12)
                    h = 1;
            }
        }
        printf("\r Clock  %2u:%02u:%02u", h, m ,s);
        fflush(stdout);
        sleep(1);
    }
    return 0;
}

To get a clock that is more robust and with more precise timing, based on your system's real-time clock, try this:

#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t now;
    time_t last_now = -1;
    struct tm *tod;
    char outbuf[32];

    while (1) {
        now = time(NULL);
        if (now != last_now) {
            last_now = now;
            tod = localtime(&now);
            strftime(outbuf,sizeof outbuf,"%l:%M:%S %P",tod);
            printf("\r Clock  %s", outbuf);
            fflush(stdout);
        }
    }
    return 0;
}

Look at the man pages for time, localtime, and strftime to see how this works, but basically it gets the time from the system clock, and if it has changed since the last time it was printed, format it and print it.

SGeorgiades
  • 1,771
  • 1
  • 11
  • 11
0

Update : I did change in printf and i got what i was looking for

Before update :

printf("\n Clock ");
       fflush(stdout);
       printf(" %02d:%02d:%02d\r", h,m,s); 
       printf("\r");
       sleep(1);

After update :

printf("\r Clock  %2u:%02u:%02u", h, m ,s);
        fflush(stdout);
        sleep(1);
    } 
Som
  • 61
  • 1
  • 6