Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What is asctime_s in C ?

Sheza Naveed

The asctime_s function converts calendar time into its textual representation. It modifies the output calendar time, unlike the asctime function. To use this function, time.h header file needs to be included as shown below:

#include <time.h>


The asctime_s function is written as shown below:

errno_t asctime_s(char *buff, rsize_t buffSize, const struct tm *timePtr)


This function takes three arguments:

  1. buff - the pointer to a buffer supplied by the user
  2. buffSize - the size of buff. This is at least 26 bytes in length
  3. timePtr - the pointer to a tm object that specifies the time to print

Return value

The return value is a static null-terminated character string. This string holds the textual representation of date and time. The asctime function will be used to get the original calendar time. The asctime function returns calendar time in the format below:

www Mmm dd hh:mm:ss yyyy


  • www - the day in three letter abbreviated form (Mon, Tue, Wed…, )
  • mmm - the month in three letter abbreviated form (Jan, * dd - the date in two digits (01, 10, 21, 30…, )
  • hh - the hour (04, 11, 12, 23…, )
  • mm - the minutes (10, 11, 12, 45…, )
  • ss - the seconds (10, 20, 30…, )
  • yyyy - the year in four digits (2000, 2010, 2016, 2021…, )


The following code shows the use of the asctime_s function. __STDC_WANT_LIB_EXT1__ is a user-defined standard to get the astime_s function to work. timePtr is being passed the local time that we wish to print. The asctime function is used to obtain the original calendar time:

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




Sheza Naveed
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring