C time() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

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);
CallWhat 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

ParameterDescription
timerOptional pointer that receives the same calendar-time value; pass NULL when it is not needed.

Return Value

Return ValueDescription
Current time_tA calendar-time representation of the current instant.
(time_t)-1The 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;
}

How It Works

  1. Step 1. Pass NULL because no output pointer is needed.
  2. Step 2. Store the returned value in time_t.
  3. Step 3. Compare with (time_t)-1 before conversion.
  4. 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;
}

How It Works

  1. Step 1. Provide the address of a time_t object.
  2. Step 2. time() writes the current value through that pointer.
  3. Step 3. The function also returns that same value.
  4. 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;
}

How It Works

  1. Step 1. Read the current calendar instant.
  2. Step 2. Convert it to local broken-down fields.
  3. Step 3. Format selected fields into caller-owned text.
  4. 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.

Next: timegm()

Continue through the C time.h function tutorials.

timegm() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful