C ctime()

The ctime() function is defined in the time.h header file. Based on the given argument this function returns the string representing localtime.


char *ctime(const time_t *timer); #where timer should be a pointer

 

ctime() Parameters: 

The ctime() function takes a single parameter 'timer' which is a pointer and it is used to set time_t object which contains a time value.


Parameter Description Required / Optional
timer  pointer to a time_t object that contains a calendar time Required

ctime() Return Value

The function returns date and time information as a  string in the format Www Mmm dd hh:mm:ss yyyy.

  • Www: represents the day in three letter abbrivated (Mon, Tue.., )
  • Mmm: represents the month in three letter abbrivated (Jan, Feb.., )
  • dd: represents the date in two digits (01, 02, 10.., )
  • hh: represents the hour (11, 12, 13…, )
  • mm: represents the minutes (10, 11, 12…, )
  • ss: represents the seconds (10, 20, 30…, )
  • yyyy: represents the year in four digits (2000, 2001, 2019…, )
Input Return Value
if parameter calendar time

 

Examples of ctime()

Example 1: Working of ctime() function in C?




#include <stdio.h>
#include <time.h>

int main()
{
   time_t timer;

   time(&timer;);

   printf("The current time is %s", ctime(&timer;));

   return(0);

}

Output:


The current time is Tue Aug 14 07:13:24 2012

Example 2: How ctime() works in C?


#include <stdio.h>
#include <time.h>

int main (){

   time_t D_time ; 
   time(& D_time); 
   clrscr(); 
   printf("%s", ctime(& D_time));
}

Output:


Sat Sep 29 05:17:05 2018