Skip to content
Related Articles

Related Articles

asctime() and asctime_s() functions in C with Examples

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 16 Dec, 2021

asctime() function: 
The asctime() function is defined in time.h header file. This function returns the pointer to the string that contains the information stored in the structure pointed to struct tm type. This function is used to return the local time defined by the system.

char *asctime(const struct tm* tm_ptr);

0Parameters: This function accepts single parameter time_ptr i.e pointer to the tm object to be converted.
Return Type: This function returns the calendar time in the form “Www Mmm dd hh:mm:ss yyyy”, where:  

  • Www: represents the day in three letter abbreviated (Mon, Tue, Wed.., )
  • Mmm: represents the month in three letter abbreviated (Jan, Feb, Mar.., )
  • dd: represents the date in two digits (01, 02, 10, 21, 31.., )
  • hh: represents the hour (11, 12, 13, 22…, )
  • mm: represents the minutes (10, 11, 12, 45…, )
  • ss: represents the seconds (10, 20, 30…, )
  • yyyy: represents the year in four digits (2000, 2001, 2019, 2020…, )

Below program demonstrate the asctime() function in C:


// C program to demonstrate
// the asctime() function
#include <stdio.h>
#include <time.h>
int main()
    struct tm* ptr;
    time_t lt;
    lt = time(NULL);
    ptr = localtime(<);
    // using the asctime() function
    printf("%s", asctime(ptr));
    return 0;


Wed Aug 14 04:21:25 2019


asctime_s() function:
This function is used to convert the given calendar time into a textual representation. We can’t modify the output calendar time in asctime() function whereas we can modify the calendar time in asctime_s() function. The general syntax of asctime_s is “Www Mmm dd hh:mm:ss yyyy“.

errno_t asctime_s(char *buf, rsize_t bufsz, 
                  const struct tm *time_ptr)

Parameters: This function accepts three parameters:  

  • time_ptr: pointer to a tm object specifying the time to print
  • buf: pointer to a user-supplied buffer at least 26 bytes in length
  • bufsz: size of the user-supplied buffer

Return Value: This function returns pointer to a static null-terminated character string holding the textual representation of date and time. The original calendar time will be obtained from the asctime() function.
Note: In some C-compilers asctime_s() won’t be supported. We can use strftime() function instead of asctime_s() function.
Below program investigates the asctime_s() function in C:


// __STDC_WANT_LIB_EXT1__ is a User defined
// standard to get astime_s() function to work
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <time.h>
int main(void)
    struct tm tm
        = *localtime(&(time_t){ time(NULL) });
    printf("%s", asctime(&tm));
// Calling C-standard to execute
// asctime_s() function
#ifdef __STDC_LIB_EXT1__
    char str[50];
    // Using the asctime_s() function
    asctime_s(str, sizeof str, &tm);
    // Print the current time
    // using the asctime_s() function
    printf("%s", str);


Wed Aug 14 04:33:54 2019



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!