Print today's date in C

This is an example C program illustrating the use of time, localtime_r, and strftime to print today's date.

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

#define SIZE 0x100

int main ()
{
    time_t t;
    char buffer[SIZE];
    struct tm ltime;

    t = time (0);
    localtime_r (& t, & ltime);
    
    strftime (buffer, SIZE, "%A, %e of %B %Y", & ltime);
    printf ("Today is %s.\n", buffer);

    return 0;
}

Download it here.

The output of the example looks like this:

Today is Monday, 12 of September 2011.

Ask and answer questions on C in the new C forum

Copyright © Ben Bullock 2009-2012. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (ben.bullock@lemoda.net) / Privacy / Disclaimer