What is ctime in C?

The ctime function returns a string containing the local time. It is based on the timer argument given to the function.

The ctime function converts the local time state to a readable text timestamp.

Library

The ctime function can be accessed via time.h library.

Parameter

The parameter of the ctime function is a pointer to a time_t object.

The time_t datatype is used to store system time values. It contains a calendar time value that is the pointer to which is given as a parameter to the function.

Syntax

char *ctime(const time_t *timer)

Return value

The ctime function returns a string of readable text containing the date and time.

Return value format

The return string has the format: Www Mmm dd hh:mm:ss yyyy

where:

Www: Day of the week
Mmm: Month of the year
dd: Day of the month
hh: Hour of the day
mm: Minute of the hour
ss: Second of the minute
yyyy: Year

Code

#include <stdio.h>
#include <time.h> // this library contains ctime function
int main() {
time_t current_time; // this variable stores current time state
time(&current_time);
printf("Time returned in the specified format is: %s", ctime(&current_time));
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved