How can I get the current date or time of day in a C program?
Submitted by: AdministratorJust use the time, ctime, localtime and/or strftime functions. Here is a simple example:
#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
time(&now);
printf("It's %s", ctime(&now));
return 0;
}
Calls to localtime and strftime look like this:
struct tm *tmp = localtime(&now);
char fmtbuf[30];
printf("It's %d:%02d:%02dn",
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
strftime(fmtbuf, sizeof fmtbuf, "%A, %B %d, %Y", tmp);
printf("on %sn", fmtbuf);
(Note that these functions take a pointer to the time_t variable, even when they will not be modifying it.
Submitted by: Administrator
#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
time(&now);
printf("It's %s", ctime(&now));
return 0;
}
Calls to localtime and strftime look like this:
struct tm *tmp = localtime(&now);
char fmtbuf[30];
printf("It's %d:%02d:%02dn",
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
strftime(fmtbuf, sizeof fmtbuf, "%A, %B %d, %Y", tmp);
printf("on %sn", fmtbuf);
(Note that these functions take a pointer to the time_t variable, even when they will not be modifying it.
Submitted by: Administrator
Read Online C Programming Job Interview Questions And Answers
Top C Programming Questions
☺ | Why does this code crash? |
☺ | What is wrong with this declaration? |
☺ | How can I read/write structures from/to data files? |
☺ | What does static variable mean in c? |
☺ | How can I increase the allowable number of simultaneously open files? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |