Thread: casting clock_t to unsigned long

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    casting clock_t to unsigned long

    I am looking at a piece of code from an exercise in the C Programming Language. It uses the clock_t data type for one of its variables. And then later when it prints the data of the variable, it does a cast to an unsigned long:

    Code:
    int main(void) {
        int testdata[MAX_ELEMENT];
        int index;                  /*  Index of found element in test data  */
        int n = -1;                 /*  Element to search for  */
        int i;
        clock_t time_taken;
    
        for ( i = 0; i < MAX_ELEMENT; ++i )
            testdata[i] = i;
    
     
        for ( i = 0, time_taken = clock(); i < 100000; ++i ) {
            index = binsearch(n, testdata, MAX_ELEMENT);
        }
    
        time_taken = clock() - time_taken;
    
        printf("binsearch() took %lu clocks (%lu seconds)\n",
               (unsigned long) time_taken,
               (unsigned long) time_taken / CLOCKS_PER_SEC);
    Is there any particular reason to cast to unsigned long here rather than just using clock_t since that is what it was initialized to?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The %lu format specification expects a corresponding unsigned long argument, not a clock_t.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    62
    printf doesn't support format specifier for clock_t?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, at least as far as I know. Maybe that has been changed in the most recent version of the C standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate unsigned long long type random numbers
    By patishi in forum C Programming
    Replies: 27
    Last Post: 09-11-2013, 09:03 PM
  2. problem while printing unsigned long long int
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 03-08-2012, 04:44 AM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. Casting unsigned long division to a double?
    By smoothdogg00 in forum C Programming
    Replies: 5
    Last Post: 12-22-2006, 09:22 AM
  5. Converting an unsigned int to to unsigned long long (64 bit)
    By ninjacookies in forum C Programming
    Replies: 18
    Last Post: 02-11-2005, 12:09 PM