- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming
Digital Clock Program in C Programming Language
In this tutorial I thought of giving you something new than regular C programming tutorial. So I decided to write article on displaying system time in C program. To understand this program you should have knowledge of structure, library function, header file and of course main function.
So what we are going to do to display current system time? We will use C program library function to get system time and using time structure we will display that time. Let’s see our logic in real code.
Code to get time in C
#include<conio.h> #include<stdio.h> #include<dos.h> int main() { struct time t; clrscr(); _setcursortype(_NOCURSOR); while(!kbhit()) { gettime(&t); gotoxy(3,3); printf("Time : %02d:%02d:%02d:%d",t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund); } return 0; }
Above is example of C Program which displays current system time and this is not a static clock program (I mean you don’t need to run every time to see updated clock time, it will keep refreshing time). It’s dynamic clock and will keep it updated (synchronized) with system time.
Let’s see how we did this. To get system time we need time structure and this structure is part of C library. You can see time structure information in below C code block.
struct time in C
struct time { unsigned char ti_min; /* minutes */ unsigned char ti_hour; /* hours */ unsigned char ti_hund; /* hundredths of seconds */ unsigned char ti_sec; /* seconds */ };
So we declared structure variable of “time” as “t” in line no 7 and we used this variable in line no 12. In line no 12 we are calling function “gettime()” and passing address of “t” as parameter or argument. The “getime()” function is part of header file “DOS.H” and it is included in line no 3. The “gettime()” function retrieves current system time and stores that information in time structure “t”. Time structure has four variables to store time information which you can see in above code block. Now we will display that information on screen (see line no. 14).
Apart from this, there are few other interesting codes in above C program. We will start with line no. 9, this line of code (“_setcursortype(_NOCURSOR)”) hides blinking cursor from screen. It’s not required for this C clock program but output will look good, you can comment this line and check the output.
Next in while loop we have condition “!kbhit()” function. This function returns non-zero (-1) value when you press any key on keyboard and returns zero if you don’t press any key. But what is the use of this function, as I told you earlier also that our clock is dynamic clock and it will keep updating itself if it is running. To achieve this, I implemented logic that we will keep our clock updating until user hits any key on keyboard, till then while loop will work as infinite loop and it will update our clock. As I told you “kbhit()” function returns zero (0) if no key is pressed on keyboard, that’s why I am applying NOT(!) operator to make it one (1) and one means true and if condition is true then loop will keep executing. But when user presses any key then it will convert that non-zero value to zero and zero means false. So loop will stop executing the code. This is how that “!kbhit()” logic works.
I think code written on line no 14 is also new to you so let me explain that also. The “gotoxy()”function is used to position the cursor on text window (output screen). It takes two integer type parameters for “x” and “y” coordinates. I have given 3 for both parameters that mean set cursor position from upper-left corner (which is equal to gotoxy(1,1)) of the window to 3 character space right and 3 character space down.
If you like it then don’t forget to share with your friends, thank you. Happy coding :)
clock with AM/PM indicator
#include<conio.h> #include<stdio.h> #include<dos.h> int main() { char *format; int hh=0; struct time t; clrscr(); _setcursortype(_NOCURSOR); while(!kbhit()) { gettime(&t); gotoxy(3,3); hh = (t.ti_hour>11)? t.ti_hour - 12 : t.ti_hour; format = (t.ti_hour>11) ? "PM" : "AM"; printf("Time : %02d:%02d:%02d %s",hh, t.ti_min, t.ti_sec, format); } return 0; }
Your opinion
Was this an interesting C language tutorial?
C programming tutorial links
- C Programming Lesson - Function in C
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program So - What is Header File and Main Function in C Programming Language
Every C complier provides a library of around 200 or more predefined functions and macros which we can use in our C program. These library functions or inbuilt functions help programmers to perform common programming task rapidly and efficiently. The - C Programming Lesson - Structures in C
We used variable in our C program to store value but one variable can store only single piece information (an integer can hold only one integer value) and to store similar type of values we had to declare many variables. To overcome this problem we u - Looping in C Programming Language, It's Types and Example
Loop is one of the important parts of C language and study of C language is incomplete without this. So let’s head towards completion of our knowledge about C language. As a dedicated C language leaner, right now you should have two questions. What i - How For Loop Works in C Programming Language
Along with while loop and do-while loop, for loop is also part of C programming language. For many programmers, for loop seems to be easiest loop comparing other two loops (while and do-while loop). Mainly its structure (syntactically) makes it easy - How While loop Works in C Programming Language
I already explained what is loop and why we use loop in c language, and I am not going to repeat that here again. So we will start with while loop explanation. While loop is one of the simple loop available in C language.It’s very easy to use, lets c - Call by Value and Call by Reference in C Programming
Call by value and call by reference is the most confusing concept among new C language programmer. I also struggled with this, the reason may be my teacher is not explaining it in simple words or I was dumb. Whatever the reason is; here I will try to - Library Functions in C Programming Language
In this C language tutorial we will learn about library function available in C. Before I start explaining about library function let me refresh your idea about function. There are two type of function available in C, user defined functions, also kno - String in C Programming Language
String in C is collection of characters whereas character in C is a single alphabet. For, example, ‘A’ is character (in C we uses single quote to assign any character to char variable) and “STRING” is a string (in C we uses double quote to assign any
© 2012 RAJKISHOR SAHU