Updating structure field value in a function - C Structure

C examples for Structure:Structure Value

Description

Updating structure field value in a function

Demo Code

#include <stdio.h>

struct time/*from  w  w w.  j ava  2  s  .com*/
{
    int hour;
    int minutes;
    int seconds;
};

int main (void)
{
    struct time timeUpdate (struct time now);
    struct time currentTime, nextTime;

    printf ("Enter the time (hh:mm:ss): ");
    scanf ("%i:%i:%i", &currentTime.hour, &currentTime.minutes, &currentTime.seconds);

    nextTime = timeUpdate (currentTime);

    printf ("Updated time is %.2i:%.2i:%.2i\n", nextTime.hour, nextTime.minutes, nextTime.seconds);

    return 0;
}
//Function to update the time by one second.

struct time timeUpdate (struct time now){
    ++now.seconds;
    if ( now.seconds == 60 )    // if next minute
    {
        now.seconds = 0;
        ++now.minutes;

        if ( now.minutes == 60 )    // if next hour
        {
            now.minutes = 0;
            ++now.hour;

            if ( now.hour == 24 )   // if next day
            {
                now.hour = 0;
            }
        }
    }

    return now;
}

Result


Related Tutorials