Introduction

A stopwatch is a useful tool for measuring time intervals. In this tutorial, we will walk you through the process of building a simple stopwatch program using the C programming language. This program will allow users to start, stop, and reset the timer, making it a functional and straightforward stopwatch.

Objective

The objective of this program is to create a simple stopwatch that can be used to track elapsed time. The program will provide basic features like starting the stopwatch, stopping it, and resetting it to zero. Additionally, we will learn how to handle user inputs and work with the system clock to measure time accurately.

Code Implementation


#include 
#include 
#include 

int main() {
    int choice;
    time_t start, end;
    double elapsed;
    
    printf("Simple Stopwatch in C\n");
    printf("1. Start Stopwatch\n");
    printf("2. Stop Stopwatch\n");
    printf("3. Reset Stopwatch\n");
    printf("4. Exit\n");
    
    while (1) {
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                start = time(NULL); // Record start time
                printf("Stopwatch started. Press 2 to stop.\n");
                break;
            case 2:
                end = time(NULL); // Record end time
                elapsed = difftime(end, start); // Calculate elapsed time
                printf("Stopwatch stopped. Elapsed time: %.2f seconds.\n", elapsed);
                break;
            case 3:
                printf("Stopwatch reset.\n");
                break;
            case 4:
                printf("Exiting stopwatch program.\n");
                exit(0);
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;
}
            

Explanation of the Program

The program begins by presenting the user with a menu to interact with the stopwatch. The available options are to start the stopwatch, stop it, reset it, or exit the program. The user selects the desired option by entering a number (1-4).

– When the user selects “1” (Start), the program records the current time using the time() function, which stores the start time in the start variable.

– When the user selects “2” (Stop), the program records the stop time and calculates the elapsed time using the difftime() function, then displays the result.

– When the user selects “3” (Reset), the stopwatch simply resets the process, allowing the user to start over.

– The “4” option exits the program and terminates the execution.

The program continues running in a loop until the user chooses to exit (option 4). If an invalid option is entered, the user is prompted to try again.

How to Run the Program

To run this C program on your system, follow these steps:

  1. Write the code in a C compiler (e.g., Code::Blocks, GCC, or Turbo C).
  2. Save the file with a .c extension, such as stopwatch.c.
  3. Compile the program. If you’re using GCC, you can use the command gcc stopwatch.c -o stopwatch.
  4. Run the program using the command ./stopwatch.
  5. Follow the on-screen instructions to use the stopwatch.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)