Microsoft KB Archive/33795

= INFO: Consecutive Calls to ctime Overwrite Previous Values =

Article ID: 33795

Article Last Modified on 7/5/2005

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft C Professional Development System 6.0a

 Microsoft C Professional Development System 6.0a

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q33795

<div class="notice_section">

<div class="notice_section">

NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

<div class="summary_section">

SUMMARY
Subsequent 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 first printf call. However, the start and finish times should be different. To correct this situation, do not call ctime twice in one call to printf. 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.

<div class="moreinformation_section">

Sample Code
/* * Compile options needed: None */


 * 1) include <stdio.h>
 * 2) include <time.h>

time_t start, finish=0;

void main(void) {

//loop until 3 seconds pass for (time(&start); finish-start < 3; time(&finish)) ;

/* If the following statement is used, the start and finish display times do not differ, both parameters get a copy of    the same buffer which is overwritten twice before entering printf: */

printf(&quot;Start time was %s and ending time was %s&quot;, ctime(&start),    ctime(&finish));

/* If the following statements are used instead, the start and finish times are different, as expected: */

printf(&quot;Start time was %s&quot;, ctime(&start)); printf(&quot;and ending time was %s\n&quot;, ctime(&finish)); }

Keywords: kbinfo KB33795

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.