Need some explanation using ctime. 
Author Message
 Need some explanation using ctime.


Quote:
> And this does not work (file oneprintf.c):

> #include <stdlib.h>
> #include <time.h>

> main() {

first of all you should make this

int main()

even if C89 allows for automatic typing, C99 does not allow
this anymore.

Quote:
>     time_t start;
>     time_t final;

>     start=975508641;
>     final=976212675;

>     printf("Start Time : %s*\nFinal Time : %s*\n",
>     ctime(&start),
>     ctime(&final)
>     );

Probably (like on my linux box) ctime returns a char pointer
to a statically allocated string. This means that anytime you
call ctime in your program the same address is returned.

Now, since it is not sure in which order the arguments to a
function or the operands of an expression are evaluated, you
can't be sure, which of the two calls to ctime is done first.
In your case, as in mine, ctime(&final) is called first, but
than ctime(&start) is called. Now the static buffer is given to the
printf function which produces the same output.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Tue, 27 May 2003 20:47:18 GMT  
 Need some explanation using ctime.

Quote:

>>     printf("Start Time : %s*\nFinal Time : %s*\n",
>>     ctime(&start),
>>     ctime(&final)
>>     );

>ctime returns a pointer to buffer for this purpose. It probably has
>only one buffer.

It's definitely allowed to use only one buffer:

    4.12.3 Time conversion functions

    Except for the strftime function, these functions return values in
    one of two static objects: a broken-down time structure and an array
    of char.  Execution of any of the functions may overwrite the
    information returned in either of these objects by any of the other
    functions.  The implementation shall behave as if no other library
    functions call these functions.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Tue, 27 May 2003 21:41:15 GMT  
 Need some explanation using ctime.

Quote:

> Could somebody clearly explain me why this work file (twoprintf.c):

> #include <stdlib.h>
> #include <time.h>

> main() {
>     time_t start;
>     time_t final;

>     start=975508641;
>     final=976212675;

It doesn't; you're not guaranteed that time_t is a long or whatever. If
this happens to give you useful times, that's by accident.

Quote:
>     printf("Start Time : %s*\n", ctime(&start));
>     printf("Final Time : %s*\n", ctime(&final));
>     exit(0);
> }

> #include <stdlib.h>
> #include <time.h>

> main() {
>     time_t start;
>     time_t final;

>     start=975508641;
>     final=976212675;

Ditto.

Quote:
>     printf("Start Time : %s*\nFinal Time : %s*\n",
>     ctime(&start),
>     ctime(&final)
>     );
>     exit(0);
> }

> Giving this wrong output :
> **************
> running oneprintf
> Start Time : Wed Nov 29 15:37:21 2000
> *
> Final Time : Wed Nov 29 15:37:21 2000
> *
> **************

That output is, barring the superfluous text which wasn't printed by
your program, correct.
As for the times being equal: ctime() puts its result in a static
buffer, which you overwrite twice in the same parameter list. Since this
invokes undefined behaviour (writing the same object twice between
consecutive sequence points), the result could be anything the compiler
likes. Instead, you get the logical result; you got lucky.

Richard



Wed, 28 May 2003 00:13:23 GMT  
 Need some explanation using ctime.
Thanks all for reply

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 28 May 2003 01:15:18 GMT  
 Need some explanation using ctime.

Quote:
> >     start=975508641;
> >     final=976212675;

> It doesn't; you're not guaranteed that time_t is a long or whatever.
If
> this happens to give you useful times, that's by accident.

In fact time clock where cut and paste from an other program output, I
know it's really bad code but it's time saving.

Quote:
> That output is, barring the superfluous text which wasn't printed by
> your program, correct.

Just cut and paste after running make !!

Thanks for answers anyway.

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 28 May 2003 01:27:11 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. '>>' on char explanation needed

2. SIMPLE EXPLANATION NEEDED.

3. Need Explanation on Sizeof

4. Need Explanation for this macro

5. extern declaration need explanation

6. need explanation on working w/ (char **)

7. i need some explanation (pointer)

8. Need an explanation

9. Bits Storage in DIBSection (only explanation needed, code already working)

10. Need explanation on COleSafeArray behaviour

11. Weird CList behaviour ? Explanation desperately needed

12. How to use CreateProcess() ? [need simple explanation/example]

 

 
Powered by phpBB® Forum Software