A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
time(NULL) returns the number of seconds elapsed since 00:00:00 UTC on January 1, 1970, not counting leap seconds. This is the Unix epoch. The return type is time_t, which is typically a signed 64-bit integer on modern systems. The NULL argument means "I do not want the result stored in a variable; just return it." You can pass a time_t pointer instead, and time will write the result there as well as returning it. The NULL form is the common idiom.
#include <stdio.h>#include <time.h>int main(void) {time_t now = time(NULL);printf("Seconds since epoch: %ld\n", (long)now);time_t stored;time_t returned = time(&stored);printf("Via pointer: %ld\n", (long)stored);printf("Return value: %ld\n", (long)returned);printf("Both equal: %d\n", stored == returned);printf("\nEpoch was: %s", "Thu Jan 1 00:00:00 1970 UTC\n");time_t y2038 = 2147483647;struct tm *overflow = gmtime(&y2038);printf("Max 32-bit time_t: %04d-%02d-%02d %02d:%02d:%02d UTC\n",overflow->tm_year + 1900, overflow->tm_mon + 1,overflow->tm_mday, overflow->tm_hour,overflow->tm_min, overflow->tm_sec);return 0;}
The Y2038 problem: on systems where time_t is a 32-bit signed integer, the timestamp overflows on January 19, 2038 at 03:14:07 UTC. The value wraps to negative, representing December 13, 1901. Modern 64-bit systems use a 64-bit time_t, pushing the overflow to the year 292 billion. If you are writing embedded code for 32-bit targets, check your time_t size.
Both work. time(NULL) is shorter and communicates that you only want the return value. time(&t) is useful when you need the result in a variable and also want to check for the error return ((time_t)-1). In practice, time(NULL) is the idiom you will see in 95 percent of C code. Use it unless you specifically need the pointer form.
time(NULL) is the starting point for every timestamp, every duration measurement, every log entry, and every cache expiration in C. Understand what it returns and what it does not return. It is not monotonic. It can jump backward if the system clock is adjusted. For monotonic timing, use clock_gettime(CLOCK_MONOTONIC). Next: strtod, for converting strings to doubles with error detection.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.