
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Timer in C++ using system calls
Here we will see how to design timer in C++ using a system call. We will not use any graphics or animations. Here timer means the stopwatch, that is up-counting the time. The used system calls are −
sleep(n) − This will help the program to sleep for n number of seconds
system() − This is used to execute the system command by passing command as an argument to this function.
Example
#include <iomanip> #include <iostream> #include <stdlib.h> #include <unistd.h> using namespace std; int hrs = 0; int mins = 0; int sec = 0; void showClk() { system("cls"); cout << setfill(' ') << setw(55) << " TIMER \n"; cout << setfill(' ') << setw(66) << " --------------------------------------\n"; cout << setfill(' ') << setw(29); cout << "| " << setfill('0') << setw(2) << hrs << " Hours | "; cout << setfill('0') << setw(2) << mins << " Minutes | "; cout << setfill('0') << setw(2) << sec << " Seconds |" << endl; cout << setfill(' ') << setw(66) << " --------------------------------------\n"; } void systemCallTimer() { while (true) { showClk(); sleep(1); sec++; if (sec == 60) { mins++; if (mins == 60) { hrs++; mins = 0; } sec = 0; } } } int main() { systemCallTimer(); }
Output
- Related Articles
- What are system calls in Operating System?
- System Calls in Unix and Windows
- Different types of system calls
- What are the different system calls in the operating system?
- How are system calls connected to the operating system?
- The most Common POSIX System Calls in Python
- The fcntl and ioctl System Calls in Python
- What is the purpose of System Calls?
- What are the Process Management System Calls?
- Differentiate between Application Programming Interfaces (APIs) and system calls.
- What are the types of system calls used in file management?
- Java Program to get the current value of the system timer in nanoseconds
- How to create timer using C++11?
- How to create a timer using tkinter?
- How to pickup & hangup calls in Android using Kotlin?

Advertisements