Wednesday, February 17, 2016

Printing the current date and time in C

Here's a little routine that prints the current time plus a given string in C:
#include <stdio.h>
#include <time.h>
#include <string.h>

void
print_log(char *arg)
{
       time_t t = time(NULL);
       char *str = ctime(&t);

       str[strlen(str) - 1] = 0;
       printf("[%s] %s\n", str, arg);
}
Which prints something like:
$ ./example
[Tue Nov 17 21:51:41 2015] this is just a test

No comments:

Post a Comment