954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A simple query

Is there any way to know the system date automatically using a c program ?
If yes please guide me through some codes !!

harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
 

There functions in such as time, localtime, and strftime.

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

int main(void)
{
   time_t now;
   if ( time(&now) != (time_t)(-1) )
   {
      struct tm *mytime = localtime(&now);
      if ( mytime )
      {
         char buffer [ 32 ];
         if ( strftime(buffer, sizeof buffer, "%x", mytime) )
         {
            printf("buffer = \"%s\"\n", buffer);
         }
      }
   }
   return 0;
}

/* my output
buffer = "06/08/05"
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You