Menu

Time macro

Cyder

Time macro

The c11 standard describes a macro called __TIME__, 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 Time is generated as follows:

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

int main() {
    time_t ti;
    struct tm *tm;
    time(&ti);
    tm = localtime(&ti);
    printf("%02d:%02d:%02d",tm->tm_hour,tm->tm_min,tm->tm_sec);
    return 0;
}

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

// Print the time of the compilation of this file
printf("Time = %s\n",__TIME__);

#undef __TIME__ // Can't undef __TIME__
#ifdef __TIME__
// 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.