ctime() Function in C/C++

The ctime() function is define in the time.h header file. The ctime() function returns the string representing the localtime based on the argument timer.

Syntax:

char *ctime(const time_t *timer)

Parameters: This function accepts single parameter time_ptr. It is used to set time_t object that contains a time value.

Return Value: This function returns a string that contains the date and time which is in human readable form.
Example:

Www Mmm dd hh:mm:ss yyyy

Note: The description of results are given below:

  • Www: Day of week.
  • Mmm: Month name.
  • dd : Day of month.
  • hh : Hour digit.
  • mm : Minute digit.
  • ss : Second digit.
  • yyyy : Year digit.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate
//  ctime() function.
#include <stdio.h>
#include <time.h>
  
int main () {
    time_t curtime;
      
    time(&curtime);
      
    printf("Current time = %s", ctime(&curtime));
      
    return(0);
}

chevron_right


Output:

Current time = Fri Sep 28 08:13:09 2018


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.