The C time() function obtains the current calendar time and returns it as a time_t value. time(NULL) is the common form when you only need the return value.
If you pass a non-null pointer, the function also stores the same value there. Calendar time represents an instant; use localtime(), gmtime(), or formatting functions to display it. Include <time.h>.
Syntax
Start with this concrete time() call:
C
time(NULL);
Call
What it does
time(NULL);
Returns the current calendar time without storing a second copy through a pointer.
Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.
Library declaration
C
time_t time(time_t *timer);
Parameters
Parameter
Description
timer
Optional pointer that receives the same calendar-time value; pass NULL when it is not needed.
Return Value
Return Value
Description
Current time_t
A calendar-time representation of the current instant.
(time_t)-1
The current calendar time is unavailable.
C Examples
Example 1 — Get Calendar Time
This example demonstrates time() with a small, complete program and checks the relevant result before using it.
C
#include <stdio.h>
#include <time.h>
int main(void) {
time_t now = time(NULL);
if (now == (time_t)-1)
printf("time unavailable\n");
else
printf("calendar time received\n");
return 0;
}
Output
calendar time received
How It Works
Step 1. Pass NULL because no output pointer is needed.
Step 2. Store the returned value in time_t.
Step 3. Compare with (time_t)-1 before conversion.
Step 4. A successful value can now be passed to other time functions.
Example 2 — Store Through a Pointer
This example demonstrates time() with a small, complete program and checks the relevant result before using it.
C
#include <stdio.h>
#include <time.h>
int main(void) {
time_t stored;
time_t returned = time(&stored);
if (returned != (time_t)-1)
printf("same value: %s\n", returned == stored ? "yes" : "no");
return 0;
}
Output
same value: yes
How It Works
Step 1. Provide the address of a time_t object.
Step 2.time() writes the current value through that pointer.
Step 3. The function also returns that same value.
Step 4. Compare both results after ruling out failure.
Example 3 — Display Current Local Time
This example demonstrates time() with a small, complete program and checks the relevant result before using it.
C
#include <stdio.h>
#include <time.h>
int main(void) {
time_t now = time(NULL);
if (now != (time_t)-1) {
struct tm *local = localtime(&now);
char text[32];
if (local && strftime(text, sizeof text, "%Y-%m-%d %H:%M", local))
printf("%s\n", text);
}
return 0;
}
Output
2026-11-17 20:15
(example value varies)
How It Works
Step 1. Read the current calendar instant.
Step 2. Convert it to local broken-down fields.
Step 3. Format selected fields into caller-owned text.
Step 4. Print only after the acquisition, conversion, and formatting checks pass.
Frequently Asked Questions
time() gets the current calendar time as a time_t value.
Include <time.h> before calling time().
On success it returns the current calendar time as time_t. On failure it returns (time_t)-1.
Do not assume time_t is always a signed integer count from a particular epoch. Use the time library to compare and convert it.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.