Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why program prints the day 2 less than normal?

#include <stdio.h>
#include <time.h>
int main(){
printf("%d\n",time(NULL));
printf("Second: %ld\n",time(NULL)/60);
printf("Minute: %ld\n",time(NULL)/(60*60));
printf("Hour: %ld\n",time(NULL)/(60*60*60));
printf("Day %ld\n",time(NULL)/(60*60*60*24));
    return 0;
}


Today is November 19. And it is 323. day of the year. When I run the program it print 321. what is the reason?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

time(NULL) returns seconds since January 1:st 1970.

Your calculations are wrong in that you assume that they are from the beginning of current the year and you divide by seconds once too many.

Amazingly, the two errors almost take each other out and you end up with a result which is nearly, but not exactly right. In fact, had you been running this program two years ago (i.e. sixty years after 1970) you would have gotten the "right" result.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading