Tuesday 4 August 2015

Make A Clock In C

A simple coded clock is useful in many potential applications. The ctime library provide a C programmer with many useful functions that allow him to keep track of how many second have passed. Use this example of a simple clock that can be used in other programs for timing operations. You can adjust the variables as needed.


Instructions


1. Type #include and #include at the top of your computer. If you want to include more functions, add them after this one.


2. Type "using namespace st;" for your next line. Remember to include the semicolon.


3. Type in int main () and include the opening and closing brackets. Because all functions in C must return a value, type return 0; at before the closing bracket.


4. Declare an integer variable that will be used to control the loop. The format should be:


int sec_counter;


5. Write the for loop. A for loop can be used, as well as a while loop.


6. Call the clock from within your loop function. You can display the results from the clock function in the time header using cout or printf, but the cout command is easier.


7. Find the number of seconds using this syntax clock()/CLOCKS_PER_SEC. (Remember that C is case sensitive.) The full syntax for the command looks like this:


cout << "Seconds Clock has been running: " << clock()//CLOCKS_PER_SEC;


The entire program should look something like this when finished:


#include


#include


using name space std;


int main ()


{


int sec_counter;


for (sec_counter = 1; sec_counter <= 32767; sec_counter++)


{


cout << Seconds Within the for loop: " << clock()/CLOCKS_PER_SEC;


};


return 0;


}

Tags: include include, like this, sec_counter sec_counter