Introduction: Simple Clock in Language C

The Idea is to create simple clock in C, but first we need to set up our software and get to know some of the things we are going to use.

Step 1: Step 1:

  1. Choose Visual Studio, Code Blocks or any other similar software (i would recommend visual studio 2015).
  2. I will use Visual Studio 2015, so type in google "Visual Studio 2015 Community", download and install.
  3. After installation, run Visual Studio, press New/Project/Console Application.
  4. In Console Application Wizard press next, then deselect Precompiled header and select Empty project, then Finish.
  5. On your right you will have Solution Explorer, right click on Source Files, Add/New Item/C++ file (.cpp), but change name to Source.c and add.
  6. Now you have C project ready to start.

Step 2: Step 2: Writing Our Code and Learning New Things

This is our code:

#include <windows.h>//we are including windows file (it's connected to function sleep()), which means that this will only work for windows, so if you're using another OS, search in google for other versions of sleep().

int main()


int h, m, s;//we add hours, minutes and seconds to our program

int D = 1000;//we add Delay of 1000 milliseconds, which makes a second and we will use that in sleep().

printf("Set time:\n");//printf writes on screen text that is inside of (" ") and \n writes in a new row.

scanf("%d %d %d", &h, &m, &s);//scanf is where we insert our time, or our values.

if (h > 12) { printf("ERROR!\n"); exit(0); } //in this if function we examine if the inserted value is bigger than 12.

if (m > 60) { printf("ERROR!\n"); exit(0); }//similar here and if it's bigger, program writes ERROR! and exits

if (s > 60) { printf("ERROR!\n"); exit(0); } //similar

while (1)//while (1) is an infinity loop and anything inside repeats itself to infinity.

s += 1;// this tells program to increase seconds for 1, everytime the while loop comes to this part.

if (s > 59) { m += 1; s = 0; }//if the seconds are more than 59, it increases the minutes and sets seconds to 0.

if (m > 59) { h += 1; m = 0; }//similar

if (h > 12) { h = 1; m = 0; s = 0; } //similar

printf("\n Clock");

printf("\n%02d:%02d:%02d", h, m, s);//this writes our time in this format "00:00:00"

Sleep(D);//this is our function sleep which slows down the while loop and makes it more like a clock.

system("cls");//this clears the screen.


return 0;


*Everything behind '//' is a comment and does not change the program, so it can be deleted.

**Visual Studio sometimes won't run the program because it consists "scanf", so you need to go to the Solution Explorer>right click on the surface>Properties (Something as in picture should pop up)>in configuration select All Configurations>Configuration properties>C/C++>Preprocessor>in Preprocessor Definitions write _CRT_SECURE_NO_WARNINGS>Save.

Step 3: Step 3: We Insret Our Time to Be Over the Set Boundaries

  1. Insert random numbers, so that the h is >12, m is > 60, s is > 60.
  2. Program writes ERROR! and exits.
  3. So far success!

Step 4: Step 4:

  1. Insert random numbers, so that the h is < 12, m is < 60, s is < 60.
  2. numbers change to 00:00:00 format and the clocks starts "ticking".
  3. SUCCESS indeed.

*After the clock passes 12, 'hours' change to 01 and 'minutes' and 'seconds' to 00.

Have fun!


alancurtis2 (author)2015-11-07

You should move the S+=1 line to before the if(S>59) line, otherwise you'll end up printing times with 60 as the seconds value when S hits 59, passes all the if statements, and then is incremented to 60 before the print statement.

EnigmaF (author)alancurtis22015-11-08

I will change that, thank you for your obeservation :)

hafiztime (author)EnigmaF2015-11-14

how to we write this code

EnigmaF (author)hafiztime2015-11-14

Well if you want to write this particular code, just copy the bolded text and you can run it, everything else i explained in steps above. But if you want to learn how to write programs in c, you should read this and also search for tutorials on youtube or anywhere else. If you have any other questions, be free to ask. :)

DIY Hacks and How Tos (author)2015-11-07

Nice C coding tutorial


Thanks :)