Introduction:

In this program, we will create a simple countdown timer that counts down from a user-specified number of seconds. The program will display the current time remaining on the screen every second. This is a basic example that introduces concepts like loops, time delay functions, and user input handling in C programming.

Objective:

The goal is to implement a countdown timer that begins from a specified number of seconds, decrementing every second, and finally prints a message when the countdown reaches zero.

Code:

#include 
#include 
#include   // for sleep() function

int main() {
    int seconds;

    // Ask the user for the number of seconds to countdown from
    printf("Enter the number of seconds to countdown from: ");
    scanf("%d", &seconds);

    // Validate if the number of seconds is positive
    if (seconds <= 0) { printf("Please enter a valid positive number.\n"); return 1; } // Countdown loop while (seconds > 0) {
        printf("%d seconds remaining...\n", seconds);
        sleep(1);  // Wait for 1 second
        seconds--;  // Decrease the countdown
    }

    // Countdown finished
    printf("Time's up!\n");
    return 0;
}

Explanation of the Program Structure:

The program begins by prompting the user to input a number of seconds to start the countdown. It then enters a while loop, where it repeatedly prints the remaining time every second using the sleep(1) function to create a delay of one second between each iteration. The loop continues until the countdown reaches zero, at which point a “Time’s up!” message is displayed.

How to Run the Program:

  1. Open a text editor and paste the C code provided above.
  2. Save the file with a .c extension (e.g., countdown_timer.c).
  3. Open a terminal or command prompt on your computer.
  4. Navigate to the directory where the C file is saved.
  5. Compile the program using a C compiler. For example, using gcc countdown_timer.c -o countdown_timer on a Linux or macOS terminal.
  6. Run the compiled program by typing ./countdown_timer in the terminal (for Linux/macOS) or countdown_timer.exe (for Windows).
  7. Input the number of seconds when prompted, and the countdown will begin.
© 2024 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.

5 thoughts on “Countdown Timer Program in C”

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)