Thread: Clock function in c

  1. #1
    Registered User
    Join Date
    Aug 2009

    Clock function in c

    Can some one tell me if this is the right way using clock

    #include <time.h>
    int main()
    int secs=0;
    clock_t  begin;
    begin = clock();
    secs = begin/CLOCKS_PER_SEC;

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Yes, that looks correct. Of course, clock() returns 0 the first time it is called (and thereafter it returns the time since it was first called) -- so secs in your example will always be zero.

    Also: you should return zero, and consider indenting your code.
    #include <stdio.h>
    #include <time.h>
    int main()
        /* . . . some complicated process . . . */
        printf("That took %d seconds.\n", clock() / CLOCKS_PER_SEC);
        return 0;

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Thanks for that, if you could answer my question on the sockets in the other post be great, as totally confused myself with the non blocking stuff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM