Unexpectedly confused by printf and ctime under MinGW using GCC

This is a discussion on Unexpectedly confused by printf and ctime under MinGW using GCC within the C Programming forums, part of the General Programming Boards category; At first glance the code seems quite simple and straightforward: test.c: Code: #include <stdio.h> #include <time.h> int main(void) { time_t ...

  1. #1
    Registered User babyifan's Avatar
    Join Date
    Sep 2009
    Location
    Shanghai
    Posts
    8

    Question Unexpectedly confused by printf and ctime under MinGW using GCC

    At first glance the code seems quite simple and straightforward:

    test.c:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
    	time_t t_0 = 1238011200;
    	time_t t_1 = 1255348800;
    
    	printf("%s", ctime(&t_0));
    	printf("%s", ctime(&t_1));
    
    	printf("Again\n");
    
    	printf("%s%s", ctime(&t_0), ctime(&t_1));
    
    	return 0;
    }
    And I expect the output to be:

    Code:
    Thu Mar 26 04:00:00 2009
    Mon Oct 12 20:00:00 2009
    Again
    Thu Mar 26 04:00:00 2009
    Mon Oct 12 20:00:00 2009

    However, after compiling under MinGW using GCC, the result is:

    Code:
    Thu Mar 26 04:00:00 2009
    Mon Oct 12 20:00:00 2009
    Again
    Thu Mar 26 04:00:00 2009
    Thu Mar 26 04:00:00 2009
    The last two record equals. Why?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because ctime has a static buffer that it uses over and over again, so subsequent calls change the value of the pointer. So that second print statement is using the same pointer value, so will print the same string.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Because you cannot do that. ctime() apparently returns a pointer to a static string (within the ctime() function). Whatever evaluates last is what gets assigned to printf().

  4. #4
    Registered User babyifan's Avatar
    Join Date
    Sep 2009
    Location
    Shanghai
    Posts
    8

    Talking

    Quote Originally Posted by tabstop View Post
    Because ctime has a static buffer that it uses over and over again, so subsequent calls change the value of the pointer. So that second print statement is using the same pointer value, so will print the same string.
    Quote Originally Posted by Kennedy View Post
    Because you cannot do that. ctime() apparently returns a pointer to a static string (within the ctime() function). Whatever evaluates last is what gets assigned to printf().
    Thanks. My understanding is, it is:

    1) the order that printf evaluates the arguments
    2) ctime holds a statically allocated memory

    that matters, right?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by babyifan View Post
    Thanks. My understanding is, it is:

    1) the order that printf evaluates the arguments
    2) ctime holds a statically allocated memory

    that matters, right?
    Because of (2), your one-shot print statement will always print the same thing twice. Which one it prints is a result of (1).

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21