Structure Assignment 17 — Initialize and Display the Time Using a Structure
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Initialize and Display the Time Using a Structure

📑 On this page:
  • Why 14:5:3 looks wrong
  • Filling a time in one line
  • Two ways to set the parts
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions

Why 14:5:3 looks wrong

Imagine a clock that says "14:5:3". Technically it's telling you the right time — 14 hours, 5 minutes, 3 seconds. But it doesn't look like a clock. Real clocks say "14:05:03". The zeros matter. And that's the whole lesson of this assignment in a nutshell: storing the time is easy, but showing it the way people expect takes a small but important formatting touch.

We're going to fill a time struct, then print it in the format a real clock would use. Along the way you'll see three ways to fill the struct — one-line, named-field, and read-from-user — and understand when each one is the right choice.

Filling a time in one line

In the last assignment, we defined the time struct and filled it with a hardcoded value. Here we look at the different ways to fill it. The simplest is the brace form: struct time t = {14, 30, 45}; — one line, three values, done. The values match the fields in order: 14 goes to hours, 30 to minutes, 45 to seconds.

There's also the named form: {.hours = 14, .minutes = 30, .seconds = 45}. It's a few extra characters, but the field names make it clear which value is which, and the order stops mattering. If you ever swap the fields in the struct definition, the named form still fills the right fields — the brace form silently puts 45 into hours and prints "45:30:14" the next time you run it. So for anything beyond two or three fields, the named form is the safer choice.

And when the time comes from the user, there's a third form: scanf("%d %d %d", &t.hours, &t.minutes, &t.seconds). Same struct, same fields, just filled at runtime. All three are worth knowing because each has its place.

💡 In one line: struct time t = {14, 30, 45}; fills in order, {.hours = 14, .minutes = 30, .seconds = 45} fills by name, and scanf fills from the user. Print with %02d:%02d:%02d for HH:MM:SS.

Two boxes side by side. Left: values 14, 30, 45 aligned with hours, minutes, seconds in order. Right: the same values but each labelled with .hours, .minutes, .seconds.

Two ways to set the parts

Here are the two brace forms next to each other. Both create the same time. The first is shorter; the second is safer.

/* Fill in order */
struct time a = {14, 30, 45};

/* Fill by name (C99) */
struct time b = {.hours = 14, .minutes = 30, .seconds = 45};
                                

Both produce the time 14:30:45. The first depends on the order of fields in the struct; the second doesn't. If you're writing code that someone else might edit later, the named form is worth the extra typing.

Style Example Good for Watch out for
In order {14, 30, 45} Small structs, quick code Breaks if the fields are reordered
By name {.hours = 14, .minutes = 30, .seconds = 45} Real projects, longer structs Needs C99 or later (fine on modern compilers)
From the user scanf("%d %d %d", &t.hours, &t.minutes, &t.seconds) Interactive programs You need to validate the input (see FAQ)

Complete C Program

Program: fill a time and print it

#include <stdio.h>

struct time {
    int hours;
    int minutes;
    int seconds;
};

int main(void) {
    struct time t = {14, 30, 45};

    printf("Time : %02d:%02d:%02d\n", t.hours, t.minutes, t.seconds);
    return 0;
}
                                    

And here are the other two forms — first with named fields, then reading from the user:

Variant 1: named fields

#include <stdio.h>

struct time {
    int hours;
    int minutes;
    int seconds;
};

int main(void) {
    struct time t = {.hours = 14, .minutes = 30, .seconds = 45};

    printf("Time : %02d:%02d:%02d\n", t.hours, t.minutes, t.seconds);
    return 0;
}
                                    

Variant 2: read from the user

#include <stdio.h>

struct time {
    int hours;
    int minutes;
    int seconds;
};

int main(void) {
    struct time t;

    printf("Enter time (hh mm ss): ");
    scanf("%d %d %d", &t.hours, &t.minutes, &t.seconds);

    printf("Time : %02d:%02d:%02d\n", t.hours, t.minutes, t.seconds);
    return 0;
}
                                    

All three versions produce a time that prints as HH:MM:SS. The first is the assignment answer. The second shows the named-field form. The third is what you'd write in a real program — ask the user, then print the time. The & in the scanf is essential; without it, scanf doesn't know where to put the numbers.

Program Output

Time : 14:30:45
                                

And for the user-input version, try single-digit parts to see the padding do its job:

Enter time (hh mm ss): 9 5 3
Time : 09:05:03
                                

The single-digit inputs 9, 5, and 3 print as 09, 05, and 03, thanks to the %02d. Without it, you'd see 9:5:3 — still readable, but not the usual time format. Try a few other times: midnight (0 0 0), noon (12 0 0), and just before midnight (23 59 59).

Step-by-Step Explanation

Short program, quick walk-through.

1. The struct

Same struct time as the last assignment. Three ints — hours, minutes, seconds. Nothing new in the definition.

2. The three fill methods

The brace form {14, 30, 45} fills in order — hours first, then minutes, then seconds. The named form {.hours = 14, .minutes = 30, .seconds = 45} fills by name, so the order doesn't matter. The scanf form fills at runtime: the user types the three numbers, and scanf puts them into the three fields via their addresses. Each method produces the same struct with the same three values.

3. Printing the time

printf("%02d:%02d:%02d\n", t.hours, t.minutes, t.seconds); — three values, three format specifiers. %02d shows two digits with leading zeros, so 9 becomes 09 and 30 stays 30. The colons between them are literal characters. The result reads like a standard time.

4. Why the padding matters

Times without padding look inconsistent: 9:5:3, 14:30:45, 0:0:0, 23:59:59. Four different widths. With padding, they all become the same width: 09:05:03, 14:30:45, 00:00:00, 23:59:59. The columns line up, the format is uniform, and it matches how clocks display times. It's a small formatting detail, but it's the kind of thing that separates a "works" program from a polished one.

5. Reading from the user

The scanf form is what you'd use in a real program. The user types "14 30 45" (or any numbers separated by spaces), and scanf reads them into the three fields. The & is important: &t.hours is the address of the hours field, which is where scanf writes the first number.

6. When to use which

Use the brace form when the time is fixed and known at compile time. Use the named form when you want extra clarity or when the struct has many fields. Use scanf when the time comes from the user. These aren't exclusive — a real program might use scanf for input, then fill a struct with named fields in a function that returns it. Pick the style that fits the situation.

7. Where this leads

Adding two times is the next assignment. The natural approach is to convert each time to seconds-since-midnight, add, then convert back. That's the same "convert to a single number" idea we used for dates. So getting comfortable with the fill patterns now saves you time later.

🎯 Remember this: fill with {14, 30, 45} or {.hours = 14, .minutes = 30, .seconds = 45} or scanf. Print with %02d:%02d:%02d for HH:MM:SS. The padding is what makes times look right.

Frequently Asked Questions

1. Does the order of values matter in the brace form?

Yes. {14, 30, 45} puts 14 into hours, 30 into minutes, and 45 into seconds, because that's the order of the fields in the struct. If you swapped the struct to declare seconds first, the same brace form would put 14 into seconds and 45 into hours — silently wrong. The named form doesn't have this problem.

2. Why %02d and not just %d?

%d prints the integer with no padding: 5 prints as "5", 14 prints as "14". %02d prints with at least two digits, padding with leading zeros: 5 prints as "05", 14 prints as "14". For times, this gives the standard two-digit format that people expect. It's a small detail that makes the output look right.

3. Can I fill the struct after declaring it?

Yes — struct time t; t.hours = 14; t.minutes = 30; t.seconds = 45; works the same. The brace form is only valid at the point of declaration. If the values come from the user, you have to fill the fields one by one (or via scanf), since they're not known at declaration time.

4. What if the user enters 99 for minutes?

The struct will accept it, but 99 minutes doesn't make sense as a time of day. A real program would validate the input: check that hours is 0-23, minutes is 0-59, seconds is 0-59, and reject the input otherwise. Or it would normalise it: 99 minutes = 1 hour 39 minutes. For this assignment, assuming valid input is fine, but the validation habit is worth adding. The next assignment (adding two times) will naturally produce out-of-range values, so you'll need to handle that.

5. What is 14:30:45 in 12-hour format?

2:30:45 PM. The rule is: if hours is 0, it's 12 AM (midnight); if hours is 1-11, it's AM; if hours is 12, it's 12 PM (noon); if hours is 13-23, subtract 12 and it's PM. So 14 becomes 2 PM, 23 becomes 11 PM. To display in 12-hour format, convert at print time: int h = t.hours % 12; if (h == 0) h = 12; printf("%02d:%02d:%02d %s", h, t.minutes, t.seconds, t.hours < 12 ? "AM" : "PM");. It's a useful exercise but not required by this assignment.

6. Can I display the time with milliseconds?

You'd add a fourth field: int milliseconds;. Same struct, same print pattern: printf("%02d:%02d:%02d.%03d\n", t.hours, t.minutes, t.seconds, t.milliseconds);. The %03d pads to three digits, so 5 milliseconds prints as 005. This is common in stopwatch and timing applications, but not in time-of-day. Keep it simple for now.

💡 Tip: After this, try reading a time from the user, then printing both the 24-hour and 12-hour versions of it. For example, "14:30:45" and "2:30:45 PM". Same struct, two different display functions. That's the kind of thing you'll do all the time once you're working with time data in real programs.

📖 Related Tutorials

  • 16. Create a Structure to Represent a Time
  • 18. Add Two Times Using a Structure
  • 14. Initialize and Display the Date Using a Structure
  • Structure Initialization in C
  • Accessing Structure Members
  • Defining a Structure
  • Array of Structures
  • All 25 Structure Assignments

Previous Topic: -->> 16. Create a Structure to Represent a Time   ||   Next Topic: -->> 18. Add Two Times Using a Structure


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.