Introduction

A Flash Sale is a limited-time promotion where items are offered at a discounted price.
To make the sale more exciting and time-sensitive, we can implement a countdown timer
that displays the remaining time until the sale ends. In this tutorial, we will write
a simple Flash Sale Countdown Timer in C programming that will display a live countdown
to a given target time, allowing customers to know how much time is left to grab the
deals before they expire.

Objective

The objective of this project is to implement a countdown timer in C programming
that simulates the countdown for a Flash Sale. It will display the remaining time
in hours, minutes, and seconds, and will update every second until the timer reaches zero.

C Program Code


#include
#include

void flashSaleTimer(int targetTime) {
time_t startTime, currentTime;
int remainingTime;

// Get current time
time(&startTime);

// Start countdown loop
while(1) {
// Get the current time
time(&currentTime);

// Calculate remaining time
remainingTime = targetTime – difftime(currentTime, startTime);

// Check if the sale has ended
if (remainingTime <= 0) {
printf(“The Flash Sale has ended!\n”);
break;
}

// Calculate hours, minutes, and seconds
int hours = remainingTime / 3600;
int minutes = (remainingTime % 3600) / 60;
int seconds = remainingTime % 60;

// Display countdown timer
printf(“\rTime left: %02d:%02d:%02d”, hours, minutes, seconds);

// Wait for 1 second before updating
sleep(1);
}
}

int main() {
// Target time for the flash sale in seconds (e.g., 2 hours = 7200 seconds)
int targetTime = 7200; // Change this value to set your target time for the sale

printf(“Flash Sale Timer\n”);
flashSaleTimer(targetTime);

return 0;
}

Program Explanation

This C program uses the standard time library to calculate and display the remaining
time for a flash sale. Here is a breakdown of how the program works:

  • time(): This function is used to get the current system time.
  • difftime(): It calculates the difference between the current time
    and the start time of the countdown.
  • sleep(1): This makes the program wait for 1 second before updating
    the timer again.
  • The program runs a loop where it continuously calculates and displays the time left
    in the format HH:MM:SS until the target time is reached.
  • Once the target time is zero, the program displays a message indicating the end of
    the Flash Sale.

How to Run the Program

  1. Save the C program to a file (e.g., flash_sale_timer.c).
  2. Open your terminal or command prompt.
  3. Navigate to the folder where the file is saved.
  4. Compile the program using a C compiler. For example, use GCC:
    gcc flash_sale_timer.c -o flash_sale_timer
  5. Run the program:
    ./flash_sale_timer
  6. The countdown timer will start and display the remaining time until the Flash Sale ends.
© 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 :)