Home > OS >  C clock program
C clock program

Time:11-16

I was trying to create a clock program with c using the eclipse ide. When I run the program it keeps executing every single count. I used system("cls"). Also tried "clear" It shows no error but it is not working.

int main(void)
{

    int hour,minute,second;
    hour=minute=second=0; 
    setbuf(stdout,NULL);  

    while(1) 
    {

        system("cls");  

        printf("d:d:d\n",hour,minute,second);  

        fflush(stdout); 

        second  ;  

        if(second==60)

        {
            minute  = 1; 
            second=0;   
        }

        if(minute==60)

        {
            hour  = 1; 
            minute=0; 
        }

        if(hour==24)

        {
            hour=0;
            minute=0;
            second=0;

        }

        sleep(1); 
    }

       return 0;

CodePudding user response:

try using system("clear");

CodePudding user response:

According to example published above you need only one line to display clock. It's easier to add carriage return:

while(1) 
{
        printf("d:d:d\r",hour,minute,second);  
        fflush(stdout); 
        ...
}
  •  Tags:  
  • c
  • Related