C ctime() Function

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

Definition

The C ctime() function converts a time_t calendar-time value to a fixed English string in local time. A typical result looks like "Sun Jan 7 14:05:09 2024\n".

It is roughly the convenient combination of localtime() and asctime(). The returned pointer refers to shared static storage and the line includes a trailing newline. Include <time.h>.

Syntax

Start with this concrete ctime() call:

C
ctime(&stamp);
CallWhat it does
ctime(&stamp);Converts stamp to local broken-down time and formats it.

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
char *ctime(const time_t *timer);

Parameters

ParameterDescription
timerPointer to the time_t calendar-time value to format.

Return Value

Return ValueDescription
String pointerPointer to a fixed-layout local-time string in static storage.
NULLThe input cannot be converted.

C Examples

Example 1 — Print the Current Time

This example demonstrates ctime() 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("%s", ctime(&now));
    }
    return 0;
}

How It Works

  1. Step 1. Ask time() for the current calendar time.
  2. Step 2. Check its failure sentinel before conversion.
  3. Step 3. Pass the address because ctime() expects a time_t *.
  4. Step 4. Print the returned local-time line; it already ends in a newline.

Example 2 — Format a Stored Timestamp

This example demonstrates ctime() 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 stamp = (time_t)0;
    char *text = ctime(&stamp);
    if (text != NULL) printf("%s", text);
    return 0;
}

How It Works

  1. Step 1. Store a sample epoch-based calendar value.
  2. Step 2. ctime() first interprets it in the local time zone.
  3. Step 3. Check that conversion returned a pointer.
  4. Step 4. The displayed date can differ by zone because the conversion is local.

Example 3 — Copy the Shared Result

This example demonstrates ctime() 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);
    char saved[32];
    char *text = ctime(&now);
    if (text != NULL) {
        snprintf(saved, sizeof saved, "%s", text);
        printf("saved: %s", saved);
    }
    return 0;
}

How It Works

  1. Step 1. Obtain one calendar-time reading.
  2. Step 2. Convert it into the library's shared static buffer.
  3. Step 3. Copy at most the destination capacity with snprintf().
  4. Step 4. Use the caller-owned copy after later time functions might replace shared storage.

Frequently Asked Questions

ctime() formats a calendar-time value as a fixed English text line.
Include <time.h> before calling ctime().
It returns a pointer to a fixed local-time string in static storage, or NULL if conversion fails. Prefer strftime().
The result is local time, fixed-format, and not thread-safe on many implementations. Prefer strftime() with localtime_r() where those APIs are available.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: difftime()

Continue through the C time.h function tutorials.

difftime() →

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