time program showing time since epoch?

This is a discussion on time program showing time since epoch? within the Linux Programming forums, part of the Platform Specific Boards category; Hi all, for a tutorial we have been given the task of demonstrating a time program that was discussed in ...

  1. #1
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    time program showing time since epoch?

    Hi all, for a tutorial we have been given the task of demonstrating a time program that was discussed in a lecture. However when I convert from the slide to try and compile it, there are many errors - many due to which there are too many spaces in between some of the functions - However there may be other parts missing.

    Code:
      #include <time. h>
      #include <stdio. h>
      #include <unistd. h>
      int main( )
    {
      int i;
      time_ t the_ time;
      for( i = 1; i <= 10; i++) 
    {
      the_ time = time(( time_ t *) 0);
      printf(" The time is %ld\ n", the_ time);
      sleep( 2);
    }
      exit( 0);
    }
    Before this code on the same slide there this explanation:
    Code:
     #include<time.h>
    time_t time(time_t *tloc);
    returns the number of seconds since epoch
    also write the returned value to a location pointed to by tloc, if this isnít a null pointer.

    Can anybody help with why it wont compile?

  2. #2
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    i have changed all the obvious spaces to this:

    Code:
      #include <time.h>
      #include <stdio.h>
      #include <unistd.h>
    
      int main( )
    {
      int i;
      time_t the_time;
      for( i = 1; i <= 10; i++) 
    {
      the_time = time((time_t *) 0);
      printf(" The time is %ld\ n", the_time);
      sleep(2);
    }
      exit(0);
    }
    Last edited by cus; 01-10-2009 at 09:12 AM. Reason: indentation

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    The spaces in "time_ t" and "the_ time" are causing it. EDIT: I see you fixed those... Now you have an extra space in '\ n' in your printf string.

    Other than that, it's badly indented (sorry, have to say it), you should be including stdlib.h, and you don't need unistd.h. Also, exiting by calling exit(0) is unnecessary -- you could just "return 0;"
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    unistd.h is necessary for the call to sleep.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by kermit View Post
    unistd.h is necessary for the call to sleep.
    Hmm. So it is.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by brewbuck View Post
    The spaces in "time_ t" and "the_ time" are causing it. EDIT: I see you fixed those... Now you have an extra space in '\ n' in your printf string.

    Other than that, it's badly indented (sorry, have to say it), you should be including stdlib.h, and you don't need unistd.h. Also, exiting by calling exit(0) is unnecessary -- you could just "return 0;"
    I didn't notice the space on the \n silly me - Thanks for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Check if a program creates prt scrs
    By Livijn in forum C# Programming
    Replies: 6
    Last Post: 09-11-2008, 06:43 AM
  3. Finding Program Running Time
    By pf732k3 in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 02:56 PM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  5. showing time
    By Micko in forum C Programming
    Replies: 1
    Last Post: 01-02-2004, 07:21 AM

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