asctime function(Convert Date and Time to ASCII) in C Language – example and detail

In the C Programming Language, the asctime feature returns a pointer to a null-terminated string that is constructed from the broken-down time pointed to with the aid of timeptr. The null-terminated string is stored in a static variable that is modified each time the asctime function is called.


The syntax for the asctime function in the C Language is:

char *asctime(const struct tm *timeptr);

Parameters or Arguments

timeptr A pointer to a tm shape that includes a time broken down into its components.


The asctime feature returns a pointer to a null-terminated string that is of the form:

Fri Feb 15 14:45:01 2013\n

Required Header

In the C Language, the required header for the asctime function is:

#include <time.h>

Applies To

In the C Language, the asctime characteristic can be used in the following versions:

ANSI/ISO 9899-1990

asctime Example

Let’s seem to be at an instance to see how you would use the asctime feature in a C program:

/* Example using asctime by */

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

int main(int argc, const char * argv[])
    /* Define temporary variables */
    time_t current_time;
    struct tm *local_time;

    /* Retrieve the current time */
    current_time = time(NULL);

    /* Get the local time using the current time */
    local_time = localtime(&current_time);

    /* Display the local time */
    printf("The time at TechOnTheNet is: %s", asctime(local_time));

    return 0;

When compiled and run, this utility will output:

The time at TechOnTheNet is: Tue Oct  2 16:19:41 2016

Similar Functions

Other C functions that are similar to the asctime function:

ctime function difftime characteristic gmtime characteristic localtime function mktime function strftime characteristic time characteristic