INFO: Consecutive Calls to ctime() Overwrite Previous ValuesLast reviewed: August 26, 1997Article ID: Q33795 |
The information in this article applies to:
SUMMARYSubsequent calls to the ctime() function overwrite the results of a previous call. The code example below demonstrates this behavior by printing the same time for the start time and finish time in its second printf() call. However, the start and finish times should be different. To correct this situation, remove the "ctime(&start)" call from the second printf() call. Then the finish time is later than the start time, as expected. This is expected behavior. The ctime() function uses one static buffer to store its results. Therefore, when the second ctime() call is made in the printf function, ctime() overwrites the value returned by the first ctime() call.
MORE INFORMATION
Sample Code
/* * Compile options needed: None */ #include <stdio.h> #include <time.h> time_t start, finish; void main(void) { int i; time(&start); for (i = 0; i < 30000; i++) time(&finish); /* If the following statement is used, the start and finish times do not differ, as expected: */ printf("Start time was %s and ending time was %s", ctime(&start), ctime(&finish)); /* If the following statements are used instead, the start and finish times are different, as expected: printf("Start time was %s", ctime(&start)); printf("and ending time was %s\n", ctime(&finish)); */ } Keywords : CRTIss kbfasttip Version : MS-DOS:5.1,6.0,6.00a,6.00ax,7.0; OS/2:5.1,6.0,6.00a; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,4.0,5.0 Platform : MS-DOS NT OS/2 WINDOWS Issue type : kbinfo |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |