INF: Consecutive Calls to ctime() Overwrite Previous Values

ID Number: Q33795

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

The sample code below prints out the same time for the start and

finish in the second printf statement; however, the statements should

be printed two seconds apart. If ctime(&start) is taken out of the

second printf statement, the finish time is later than the start time,

as expected.

As stated in the Microsoft C 5.1 README.DOC and the Microsoft C 6.0,

6.0a, 6.0ax, and C/C++ version 7.0 online help, the ctime function

uses a single static buffer to store the results of the call (that is,

when the second call to the function is made, the results of the first

call are destroyed). Therefore, the behavior of the example is

expected.

More Information:

The following sample code illustrates this behavior:

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

#include <time.h>

time_t start, finish;

void main(void) {

int i;

time(&start);

printf("the time is %s\n", ctime(&start) );

for ( i =0; i<1000; i++ )

time(&finish); /* Start and finish should be about 2 sec apart. */

printf("ending time is %s and %s\n", ctime(&start), ctime(&finish));

}

Additional reference words: 5.10 6.00 6.00a 6.00ax 7.00