C Program Using time_struct to Display Time in HH:MM:SS Format

Structure Definition: time_struct

Here’s how you define the structure to hold time-related information:

struct time_struct {
int hour;
int minute;
int second;
};

Objective

  • Define a structure named time_struct
  • Members: hour, minute, second (all integers)
  • Assign values manually
  • Print time in format HH:MM:SS

Full C Program Example

#include <stdio.h>

// Define the time structure
struct time_struct {
int hour;
int minute;
int second;
};

int main() {
// Declare and assign values
struct time_struct currentTime;

currentTime.hour = 16;
currentTime.minute = 40;
currentTime.second = 51;

// Display the time in HH:MM:SS format
printf("Current Time: %02d:%02d:%02d\n", currentTime.hour, currentTime.minute, currentTime.second);

return 0;
}

Sample Output

Current Time: 16:40:51

Explanation

  • The structure groups time components under a single variable.
  • Values are manually assigned to each member.
  • The format specifier %02d ensures that single-digit numbers are displayed with a leading zero for proper formatting.

Real-Life Use Case

Using a time_struct like this is ideal when:

  • Building clock or timer programs.
  • Working with logging systems that record time.
  • Structuring time data in scheduling or alarms.

Tip

You can also create an array of time_struct to store multiple timestamps or use functions to manipulate time values in a modular program.


Conclusion

This program clearly demonstrates how to define a structure and use it to format time in C. The time_struct structure makes your code more readable, modular, and scalable—perfect for real-world time-based logic.

Mastering C structures like this one is essential for building real-world applications that manage related data types.

Previous Article

C Program Using struct personal to Read and Print Employee Data

Next Article

C Program Using student_record Structure to Store and Display Student Details

Write a Comment

Leave a Comment

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

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨