Example from book - time(NULL) doesn't work

This is a discussion on Example from book - time(NULL) doesn't work within the C Programming forums, part of the General Programming Boards category; I'm reading a book of Micheal Vine, "C For The Absolute Beginner" (because I am an absolute beginner, actually) and ...

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    Example from book - time(NULL) doesn't work

    I'm reading a book of Micheal Vine, "C For The Absolute Beginner" (because I am an absolute beginner, actually) and there is this piece of code, which is an example from the book, but it doesn't work.

    It is a game where you have to memorize numbers and be able to remember them. It should display the numbers for a while and then clear the screen. The problem is that it doesn't wait for a while. It shows the numbers and clears the screen right away.

    Here's the entire code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
        /* Variable definitions. {{{ */
        char c_Yes_No = '';
        int i_Resp1 = 0;
        int i_Resp2 = 0;
        int i_Resp3 = 0;
        int i_Elapsed_Time = 0;
        int i_Current_Time = 0;
        int i_Random_Number = 0;
        int i_1 = 0;
        int i_2 = 0;
        int i_3 = 0;
        int i_Counter = 0; /* Variable definitions end }}} */
    
        srand(time(NULL));
    
        printf("
    Jogar o jogo da concentração? (s, n)");
        scanf("%c", &c_Yes_No);
    
        if (c_Yes_No == 's' || c_Yes_No == 'S') { /* {{{ */
        
            i_1 = rand() % 100;
            i_2 = rand() % 100;
            i_3 = rand() % 100;
    
            printf("Se concentre nos três números.");
            printf("%d    %d    %d", i_1, i_2, i_3);
    
            /* This DOES NOT work. */
            do {
                i_Elapsed_Time = time(NULL);
            } while ((i_Elapsed_Time - i_Current_Time) < 3);
            
            /* This work. (I tried this myself. It is not from the book. */
            /* sleep(3); */
    
            system("clear");
    
            printf("Digite cada número separado por um espaço: ");
            scanf("%d %d %d", &i_Resp1, &i_Resp2, &i_Resp3);
    
            if (i_1 == i_Resp1 && i_2 == i_Resp2 && i_3 == i_Resp3) { /* {{{ */
            
                printf("Parabéns! Acertou todos.");
            }
            else {
                printf("Os números corretos eram %d, %d, e %d", i_1, i_2, i_3);
            } /* End child if. }}} */
    
        } /* End 'if'. }}} */
    
        return 0;
    } /* End of main() function. */
    
    /* 
     * vim:foldmethod=marker foldmarker={{{,}}}
     */
    This is the piece of code that should make it wait:

    Code:
            /* This DOES NOT work. */
            do {
                i_Elapsed_Time = time(NULL);
            } while ((i_Elapsed_Time - i_Current_Time) < 3);
    Of course the 'sleep()' function works. However, I would very much like to understand the piece of code with time(NULL) as shown in the book and if is supposed to work, why it doesn't.

    Thanks in advance.
    Last edited by FernandoBasso; 10-30-2011 at 04:25 AM.

  2. #2
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Edit: Forget about this, I was mistaken here.

    Edit2: doesn't time() return millisconds? Three milliseconds isn't a very long time...
    Last edited by dennis.cpp; 10-30-2011 at 04:35 AM.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  3. #3
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    The problem was in the logic, I think. i_Elapsed_Time was being reassigned a value in every iteration of the do-while loop. That way i_Elapsed_Time - i_Current_Time would always be zero and would never get bigger (or equal) than 3.

    First post code:
    Code:
    /* This DOES NOT work. */         
    do {             
        i_Elapsed_Time = time(NULL);         
    } while ((i_Elapsed_Time - i_Current_Time) < 3);
    I just moved i_Elapsed_Time = time(NULL) outside the loop
    and it worked.
    Code:
            i_Elapsed_Time = time(NULL);
            do {
                /* do nothing. */
            } while ((time(NULL) - i_Elapsed_Time) < 3);
    I just don't understand why the guy didn't just use sleep(3) in
    the book example.

    Solved. I think it was a little oversight in the book.
    Last edited by FernandoBasso; 10-30-2011 at 05:59 AM. Reason: Solved.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by FernandoBasso View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    #include <time.h>

    time() works in seconds.

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    I just don't understand why the guy didn't just use sleep(3) in
    the book example.
    There is no sleep() function in standard C, so perhaps that's why...

    But then again, he's assuming time() returns a value in seconds, which it is not required to, so perhaps he's not overly concerned with standard C.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    The time() function returns a time_t which does usually assume seconds.
    From the time_t:
    It is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. This is due to historical reasons, since it corresponds to a unix timestamp, but is widely implemented in C libraries across all platforms.
    Jim

  7. #7
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    The time() function returns a time_t which does usually assume seconds.
    Which no one has disputed. It usually is in seconds. I would expect a good book to make a note that it is not required to be in seconds, even if it then uses code that assumes it is in seconds.

    This assumption is not generally necessary, however, because of the difftime() function, which I would expect an even better book to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. first time compiling with external .c and .h file, doesn't work
    By Adam Rinkleff in forum C Programming
    Replies: 17
    Last Post: 06-21-2011, 05:38 PM
  2. Replies: 1
    Last Post: 12-07-2010, 05:53 AM
  3. Replies: 3
    Last Post: 03-07-2003, 08:06 PM
  4. Time Delay doesn't work!
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-17-2001, 12:12 PM

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