Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to get current year in C OnlineGDB

0 votes
asked May 28, 2020 by Adisak Suasaming (120 points)
how to get current year in C OnlineGDB

3 Answers

0 votes
answered May 29, 2020 by shyam (570 points)

#include <stdio.h>
#include <time.h>
int main()
{
    time_t seconds=time(NULL);
    struct tm* current_time=localtime(&seconds); 
    
    printf("Current year = %d\n",(current_time->tm_year + 1900));
    return 0;
}

time(NULL) function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds.

tm_year indicates the number of the current year starting from 1900. So, to print the actual value of the current year, you will have to add 1900 to its value.

You need to import time.h header file to use these features. 

0 votes
answered May 29, 2020 by Teja Reddy Bhumi Reddy (140 points)

#include <stdio.h>
#include <time.h>
int main()
{
    time_t seconds=time(NULL);
    struct tm* current_time=localtime(&seconds); 
    
    printf("Current year = %d\n",(current_time->tm_year + 1900));
    return 0;
}

0 votes
answered May 29, 2020 by LiOS (6,420 points)
A cool way is to use predefined macros in C. These include time, data, file etc encompassed between 2x underscores either side e.g. __TIME__

Below code prints only the year of current date:

#include <stdio.h>
int main()
{
    printf("%s", __DATE__ + 7); //usually prints 11 byte string, but this points to 8th byte to print year
    
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...