w3resource

C ctime() function

C ctime() function - Convert a time value to a date and time string

The ctime() function is used to convert the time value pointed to by time to local time in the form of a character string. A time value is usually obtained by a call to the time() function.

The function is equivalent to: asctime(localtime(clock)).

Syntax:

char *ctime(const time_t *timer)

Parameters:

Name Description Required /Optional
timer Pointer to stored time to convert. Required

Return value from ctime()

  • The ctime() function returns a pointer to the character string result.
  • If the function is unsuccessful, it returns NULL. 

Example: ctime() function

The following example prints a message giving the current date and time:

#include <time.h>
#include <stdio.h>
 
int main(void)
{
   time_t ltime;

   time(<time); 

   printf("Current date and time is %s", ctime(<time));
}

Output:

Current date and time is Tue Dec 20 09:05:22 2022

C Programming Code Editor:

Previous C Programming: C clock()
Next C Programming: C difftime()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming/time/c-ctime.php