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);
Call
What 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
Parameter
Description
timer
Pointer to the time_t calendar-time value to format.
Return Value
Return Value
Description
String pointer
Pointer to a fixed-layout local-time string in static storage.
NULL
The 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;
}
Output
Tue Nov 17 20:15:00 2026
(example value varies)
How It Works
Step 1. Ask time() for the current calendar time.
Step 2. Check its failure sentinel before conversion.
Step 3. Pass the address because ctime() expects a time_t *.
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.
saved: Tue Nov 17 20:15:00 2026
(example value varies)
How It Works
Step 1. Obtain one calendar-time reading.
Step 2. Convert it into the library's shared static buffer.
Step 3. Copy at most the destination capacity with snprintf().
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.