YouTip LogoYouTip

C Examples Time Structure

C Example - Calculate the Difference Between Two Time Periods | Tutorial

Tutorial -- Learning is not just about technology, but also about dreams!

C Tutorial

C Tutorial C Introduction C Environment Setup C VScode C Program Structure C Basic Syntax C Data Types C Variables C Constants C Storage Classes C Operators C Decision Making C Loops C Functions C Scope Rules C Arrays C enum(Enumeration) C Pointers C Function Pointers & Callback Functions C Strings C Structures C Unions C Bit Fields C typedef C Input & Output C File I/O C Preprocessors C Header Files C Type Casting C Error Handling C Recursion C Variable Arguments C Memory Management C Undefined Behavior C Command Line Arguments C Safe Functions C Sorting Algorithms C Project Structure C Examples C Classic 100 Examples C Quiz

C Standard Library

C Standard Library - Reference Manual C Standard Library - <assert.h> C Standard Library - <ctype.h> C Standard Library - <errno.h> C Standard Library - <float.h> C Standard Library - <limits.h> C Standard Library - <locale.h> C Standard Library - <math.h> C Standard Library - <setjmp.h> C Standard Library - <signal.h> C Standard Library - <stdarg.h> C Standard Library - <stddef.h> C Standard Library - <stdio.h> C Standard Library - <stdlib.h> C Standard Library - <string.h> C Standard Library - <time.h> C Standard Library <stdbool.h> C Standard Library <stdint.h> C Standard Library <inttypes.h> C Standard Library <complex.h> C Standard Library <tgmath.h> C Standard Library <fenv.h>

C Project Structure

C Classic 100 Examples

Deep Dive

Computer Science

Software

Programming

Development Tools

Scripting

Web Design & Development

Programming Languages

Scripting Languages

Web Service

Network Services

C Example - Calculate the Difference Between Two Time Periods

C Examples C Examples

Calculate the difference between two time periods.

Example

#include<stdio.h>

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

void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);

int main()
{
    struct TIME startTime, stopTime, diff;

    printf("Enter start time: n");
    printf("Enter hours, minutes, seconds: ");
    scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);

    printf("Enter stop time: n");
    printf("Enter hours, minutes, seconds: ");
    scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);

    // Calculate difference
    differenceBetweenTimePeriod(startTime, stopTime, &diff);

    printf("nTime Difference: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
    printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
    printf("= %d:%d:%dn", diff.hours, diff.minutes, diff.seconds);

    return 0;
}

void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
    if(stop.seconds > start.seconds)
    {
        --start.minutes;
        start.seconds += 60;
    }

    diff->seconds = start.seconds - stop.seconds;

    if(stop.minutes > start.minutes)
    {
        --start.hours;
        start.minutes += 60;
    }

    diff->minutes = start.minutes - stop.minutes;
    diff->hours = start.hours - stop.hours;
}

The output is:

Enter start time: 
Enter hours, minutes, seconds: 12 34 55
Enter stop time: 
Enter hours, minutes, seconds: 8 12 5

Time Difference: 12:34:55 - 8:12:5 = 4:22:50

C Examples C Examples

← C Examples Read FileC Examples Structure Store Inf β†’