Find the answer to your Linux question:
Results 1 to 6 of 6
Hi guys, I'm supposed to display a clock using a C program.I looked up a few date and time functions in C and came up with this program... Code: #include ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20

    Displaying the clock using C

    Hi guys,

    I'm supposed to display a clock using a C program.I looked up a few date and time functions in C and came up with this program...

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define SIZE 256
    int main()
    {
    char buffer[SIZE];
    time_t curtime;
    struct tm *loctime;
    curtime = time(NULL);
    
    loctime=localtime(&curtime);
    strftime(buffer,SIZE,"%a,%b, %I:%M:%S %p",&loctime);
    fputs(buffer,stdout);
    }
    However all this ever does is that it displays the Hour:Minutes:Seconds in a sort of static fashion.

    I would like it to be dynamic and more of a clock.

    Can someone help me out with the solution

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Are you trying to learn C programming on your own, or is this for a class?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20
    No it is not a class exercise.....Its actually part of a project.....

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    So, besides such a general description of your "task", why don't you post your desired output characteristics (requirements) here. Do note that main() is a function that returns an integer and you are not returning anything - return a 0 value at the very least. In any case, your code only gets the current date+time and displays it once. If you want a real updating clock, especially one that keeps the date+time displayed at a particular location on either the screen or on a terminal, then you need to put your code in a loop, display it using some screen formatting library such as ncurses (terminal) or Qt/wxWin/GTK+/etc (GUI desktop display), sleep some period of time, and then repeat the loop. You can see the code for the xclock easily enough for a raw xlib version that does just this.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20
    Thanks for your advice.

    I looked up a few manuals on ncurses but still couldn't get a very good idea on it.It would be nice if you could help me out with a program to illustrate this.

    Thanks.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    First, read the man pages. O'Reilly Publishing has a Nutshell book on ncurses programming. You can also find some example programs that use it on the net - try google.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...