C Language: asctime function
(Convert Date and Time to ASCII)
In the C Programming Language, the asctime function returns a pointer to a null-terminated string that is constructed from the broken-down time pointed to by timeptr. The null-terminated string is stored in a static variable that is modified each time the asctime function is called.
Syntax
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 structure that contains a time broken down into its components.
Returns
The asctime function 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 function can be used in the following versions:
- ANSI/ISO 9899-1990
asctime Example
Let's look at an example to see how you would use the asctime function in a C program:
/* Example using asctime by TechOnTheNet.com */ #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(¤t_time); /* Display the local time */ printf("The time at TechOnTheNet is: %s", asctime(local_time)); return 0; }
When compiled and run, this application 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:
Advertisements