Menu

Date macro

Cyder

Date macro

The c11 standard describes a macro called __DATE__, that always expands to a string constant the the current time.
It is a predefined macro, that cannot be undefined or redefined.
The string returned by Date is generated as follows:

#include <stdio.h>
#include <time.h>
static char const month_names[12][4] = {
 "Jan","Feb","Mar","Apr","May","Jun",
 "Jul","Aug","Sep","Oct","Nov","Dec"};

int main() {
    time_t ti;
    struct tm *tm;
    time(&ti);
    tm = localtime(&ti);
    printf("%.3s %2d %d",month_names[tm->tm_mon],tm->tm_mday,tm->tm_year+1900);
    return 0;
}

Hint: __DATE__ is returned as a normal string token, thus allowing it to be used for string concantation.

// Print the date of the compilation of this file
printf("Date = %s\n",__DATE__);

#undef __DATE__ // Can't undef __DATE__
#ifdef __DATE__
// This block will still be enabled
#endif

s.a. [Conditional directives]
s.a. [Macros]


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.