Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple c program does not produce required output

Tags:

c

time

I am using the following program to print the current time

int main() 
{
  printf("%s",__TIME__);
  return 0;
}

It works only for the first time. If I run it after sometime it again gives the same old time.

Why do I need to do to refresh the time ?

like image 925
user657247 Avatar asked Mar 13 '11 05:03

user657247


1 Answers

__TIME__ is a standard predefined macro that expands to a string constant that describes the time at which the preprocessor is being run.

It is replaced by the preprocessor just before the compilation. So it does not change with different runs. But if you recompile your program you'll see the change.

To get the current time of the day you can use the time, localtime and asctime functions as:

time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
like image 196
codaddict Avatar answered Nov 15 '22 08:11

codaddict