0

I'm trying to make a simple clock using c. I want the values of the numbers to change alone without having to print the whole statement forever.

Output I'm looking for:

 Hour => {A changing number}
 Minute => {A changing number}
 Second => {A changing number}

I tried this:

    #include <stdio.h>
    int clock(int h,int m,int s){ 
            if(h<=60 && m<=60 && s<=60){
                    while(1){
                            s++;
                            if(s>60){
                                    m++;
                                    s = 0;    
                            }
                            if(m>60){
                                    h++;
                                    m=0;
                            }
                            printf("Hour => %d\nMinute => %d\nSecond => %d\n",h,m,s);
                    }   
            }           
            return 0;
    }
    int main(){
            int h,m,s;
            printf("Enter an hour: \n");
            scanf("%d", &h);
            printf("Enter an minutes: \n");
            scanf("%d", &m);
            printf("Enter an seconds: \n");
            scanf("%d", &s);
            clock(h,m,s);
            return 0;
    }

Note: I'm a new computer science student and I'm new to c too so forgive me if I have some mistakes or methods that are not productive in my code.

salluc 1
  • 45
  • 8
  • are you saying you only want the statement to print when one of hours, minutes, and/or seconds changes? `while(1)` is going to fly, if you want anything resembling a clock, trying [`sleep`](https://man7.org/linux/man-pages/man3/sleep.3.html)ing for a second each loop iteration. – yano Nov 02 '21 at 14:43
  • You can use `ncurses` for this, to move the cursor to a certain spot on the screen and then print something just there. – Steve Summit Nov 02 '21 at 14:43
  • 2
    In standard C, you can't "change already printed" text. You can use carriage returns instead of newlines to "overwrite" a line (see https://stackoverflow.com/questions/15192441/update-printf-value-on-same-line-instead-of-new-one), or you can use platform specific functions or a library like ncurses. – lulle2007200 Nov 02 '21 at 14:45
  • yes this is what I want @yano – salluc 1 Nov 02 '21 at 14:46
  • To me it's unclear what you are asking. Is it a) How to avoid that your clock changes much fast than a real clock? or is it b) How to avoid printing a new line every time the values changes? or is it c) both? – Support Ukraine Nov 02 '21 at 14:50
  • it's b @4386427 – salluc 1 Nov 02 '21 at 14:51
  • Once you get your numbers printing where you want them, you can achieve a crude update rate by calling `sleep(1)` between each tick of the clock. (It'll lose time, but it's a start.) – Steve Summit Nov 02 '21 at 14:51
  • yes I know that and I know how to fix it using sleep but what I'm looking for is a way to avoid printing a new line every time the values changes @yano – salluc 1 Nov 02 '21 at 14:54
  • 1
    use \r in printf –  Nov 02 '21 at 14:55
  • @lulle that's an answer – user253751 Nov 02 '21 at 14:57
  • I tried it but did not work @Mr_Robot – salluc 1 Nov 02 '21 at 15:04
  • oh that is working now I have used it the wrong way the first time. Thank you. @Mr_Robot – salluc 1 Nov 02 '21 at 15:30
  • welcome @salluc1 –  Nov 02 '21 at 17:35

1 Answers1

1

If you are willing to print the time on a single line instead of 3 lines, you can try like:

int my_clock(int h,int m,int s)
{
  if(h<=60 && m<=60 && s<=60)
  {
    while(1)
    {
      s++;
      if(s>60)
      {
        m++;
        s = 0;
      }
      if(m>60)
      {
        h++;
        m=0;
      }

      // Go to start of current line
      printf("\r");

      // Overwrite all
      printf("                                                            ");

      // Go to start of current line
      printf("\r");

      // Print the new time
      printf("Hour => %d Minute => %d Second => %d ",h,m,s);
      fflush(stdout);

      // Wait 1 sec
      sleep(1);
    }
  }
  return 0;
}

I'm however not sure all terminals will support the \r as "Go to start of current line"

yano
  • 4,827
  • 2
  • 23
  • 35
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63