C Program to Make a Digital Clock Using C Graphics

Here is a C graphics program to make a digital clock using graphics.h header file. In this program, we will make a digital clock that print current Day, Month, Year and Date on screen inside a rectangular box. We will use below mentioned graphics functions in this program. Here, we are using time function of time.h header file to get the current epoch time(epoch time is number of seconds since 1 January 1970, UTC).

We convert epoch time to string representing the localtime in "www mmm dd hh:mm:ss yyyy" format, where www is the weekday, mmm is the month in letters, dd is the day of the month, hh:mm:ss is the time, and yyyy is the year. After printing current date and time on screen, it waits for 1000 milliseconds(1 second) before printing it again on screen.

We will use below mentioned graphics functions in this program.


Function Description
initgraph initgraph is a crucial function in Turbo C graphics that initializes the graphics system. It sets up the graphics mode, graphics driver, and the location of the BGI (Borland Graphics Interface) driver file. This function must be called before any graphics operation is performed.
setcolor The setcolor function is used to set the drawing color for subsequent graphics operations. It takes an integer argument representing the color code. Turbo C supports a predefined set of color constants such as WHITE, RED, GREEN, etc.
setfillstyle setfillstyle sets the fill pattern and color for subsequent filled shapes. It takes two arguments: the fill style and the fill color. The fill style can be patterns like SOLID_FILL, LINE_FILL, etc., and the fill color is specified using color constants.
rectangle The rectangle function is used to draw a rectangle on the screen. It takes two pairs of coordinates, representing the diagonally opposite corners of the rectangle, and draws the outline of the rectangle.
floodfill floodfill is a flood-fill algorithm that fills an enclosed area with the current drawing color, starting from a specified point. It is often used after drawing shapes to fill them with color.
settextstyle The settextstyle function sets the font style and size for text that will be drawn on the screen. It takes three arguments: font, direction, and size.
closegraph closegraph terminates the graphics mode and deallocates any system resources that were acquired during the initialization of the graphics system. It should be called before exiting a graphics program.

C program to make a digital clock using graphics

#include <graphics.h>
#include <time.h>
#include <dos.h>
#include <string.h>

int main() {
    int gd = DETECT, gm;
    int midx, midy;
    long current_time;
    char timeStr[256];

    initgraph(&gd, &gm, "C:\\TC\\BGI");

    /* mid pixel in horizontal and vertical axis */
    midx = getmaxx() / 2;
    midy = getmaxy() / 2;

    while (!kbhit()) {
        cleardevice();
        setcolor(WHITE);
        setfillstyle(SOLID_FILL, WHITE);
        rectangle(midx - 250, midy - 40, midx + 250, midy + 40);
        floodfill(midx, midy, WHITE);
        /* Get Current epoch time in seconds */
        current_time = time(NULL);
        /* store the date and time in string */
        strcpy(timeStr, ctime(&current_time));
        setcolor(RED);
        settextjustify(CENTER_TEXT, CENTER_TEXT);
        settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);

        moveto(midx, midy);
        /* print current time */
        outtext(timeStr);
        /* Add delay of 1000 milliseconds(1 second) */
        delay(1000);
    }

    closegraph();
    return 0;
}
Output Here is the screen shot of digital clock that shows current date and time on screen. C graphics program to make a digital clock

Conclusion

In conclusion, this tutorial has guided you through the process of creating a dynamic and visually engaging digital clock using Turbo C graphics. By delving into fundamental graphics functions, you've not only learned how to set up the Turbo C environment and draw text on the screen but also implemented a real-time clock that continuously updates to display the current time.

Graphics programming offers a unique intersection of logic and creativity, and Turbo C, with its nostalgic charm, provides an ideal playground for exploring these concepts. The knowledge gained from this tutorial forms a strong foundation for further exploration into the expansive realm of visual programming.

As you advance in your graphics programming journey, consider expanding the digital clock project by incorporating additional features such as custom fonts, backgrounds, or even dynamic animations. Turbo C graphics, though considered a classic, imparts valuable insights into graphics principles that remain relevant in contemporary programming environments.

The skills honed through this tutorial are transferable, empowering you to explore other graphics libraries and frameworks with confidence. Whether you're driven by a passion for creating aesthetically pleasing applications or aiming to develop interactive displays, Turbo C graphics provides a nostalgic yet robust platform for your creative endeavors.

Remember, graphics programming is an art form that thrives on experimentation and innovation. Use this newfound knowledge to breathe life into your ideas on the digital canvas. Embrace the possibilities, and let your imagination flourish in the dynamic world of visual programming. Happy coding!


Related Topics
C program to draw 3D bar graph on screen
C program to draw concentric circles
C program to draw cosine(cos) wave graph
C program to draw pie chart using graphics
C Program to draw stars in night sky
C program for bouncing ball animation
C graphics program for moving car animation
List of all C Graphics Programs