Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
C Time functions from time.h


Just a few words about the problems. Both localtime and asctime return pointers to 'static-duration' data. Thus, somehow there is allocated fixed space in memory to the results returned by these two functions. This saves memory space, and it saves time because we do not need to allocate and deallocate tm structs and text strings. But, it calls for a very disciplined use of the functions: Either you should use (print, for instance) the results of the functions right away, or the results should be copied to your own variables before they are overwritten by subsequent calls of the time functions.