w3resource logo


C Programming Exercises

C Exercises: Compute the number of seconds passed since the beginning of the month

Secondary Nav

C Date Time: Exercise-2 with Solution

Write a program in C to compute the number of seconds passed since the beginning of the month.

Expected Output :

475179 seconds passed since the beginning of the month.

C Code:

#include <stdio.h>
#include <time.h>
 
int main(void)
{
    time_t now;
    time(&now);
 
    struct tm beg_month;
    beg_month = *localtime(&now);
    beg_month.tm_hour = 0;
    beg_month.tm_min = 0;
    beg_month.tm_sec = 0;
    beg_month.tm_mday = 1;
 
    double seconds = difftime(now, mktime(&beg_month));
    printf("\n %.f seconds passed since the beginning of the month.\n\n", seconds);
    return 0;
}

Flowchart :

Flowchart: Compute the number of seconds passed since the beginning of the month

C Practice online:

#include <stdio.h>
#include <time.h>
 
int main(void)
{
    time_t now;
    time(&now);
 
    struct tm beg_month;
    beg_month = *localtime(&now);
    beg_month.tm_hour = 0;
    beg_month.tm_min = 0;
    beg_month.tm_sec = 0;
    beg_month.tm_mday = 1;
 
    double seconds = difftime(now, mktime(&beg_month));
    printf("\n %.f seconds passed since the beginning of the month.\n\n", seconds);
    return 0;
}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number