I just started c++ programming in college and there was a problem in my homework that said something about getting the total number of seconds elapsed since January 1, 1970. I think I found the answer here: http://www.cplusplus.com/reference/clibrary/ctime/time/ but the thing is that I don't want to just copy the answer; I want to understand it. If someone could please take the time to fully explain this to me. The reason I don't ask my professor is because this is a "bonus" problem and he won't explain it until later. Here are the exact instructions:
If you include time.h, you can get the number of seconds that have elapsed since Jan. 1, 1970 using time(NULL). Write a program that calculates the current date and time using this number. You cannot use any other functions other than time(NULL).
And after fixing the code from the link a bit, this is my final answer:
1 2 3 4 5 6 7 8 9 10 11 12
#include <time.h>
#include <stdio.h>
int main ()
{
time_t seconds;
seconds = time (NULL);
printf ("%ld seconds since January 1, 1970");
return 0;
}
I would greatly appreciate if someone would explain how this works. I've only worked with the iostream library, so that's why I'm so confused with these new commands. Thanks in advance.
Actually, I asked my professor if using <stdio.h> was OK and he said no, only <time.h> and <iostream>, so this code is not the right answer. If you could please explain to me how time(NULL) works, that would be great. He told me that I could use
int a = time(NULL)
to help me find the answer, and from there I could just figure it out. Thanks so much for your help, in advance. :D