C Get Current Year: Quick Guide

In C language, the current year can be obtained by using the time() function in the time.h header file, and accessing the year through the tm_year member of the tm structure. Below is an example code for obtaining the current year.

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

int main() {
    time_t t = time(NULL);
    struct tm *now = localtime(&t);
    int year = now->tm_year + 1900;

    printf("Current year is: %d\n", year);

    return 0;
}

In the provided sample code, the current timestamp is first obtained using the time() function, then converted to a tm structure using the localtime() function, and finally the current year is retrieved by adding 1900 to the tm_year member.

bannerAds