Need Help With This Code

This is a discussion on Need Help With This Code within the C Programming forums, part of the General Programming Boards category; http://cboard.cprogramming.com/showt...715#post368715 can anybody pls help me with this code, it runs fine but i can't understand what is happening a ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    12

    Exclamation Need Help With This Code

    http://cboard.cprogramming.com/showt...715#post368715

    can anybody pls help me with this code, it runs fine but i can't understand what is happening

    a good explanation(line by line) would be help full

    thnx

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
       time_t now; //declare the new variable
    
    	//call the time(00 function to initialize variable 
    	//with the current time and check the return value
       if ( time(&now) != (time_t)(-1) ) 
       {
          struct tm *mytime = localtime(&now);//convert the time to the local zone
          if ( mytime ) //if conversion was successful
          {
             char buffer [ 32 ];
             if ( strftime(buffer, sizeof buffer, "%X", mytime) ) //print the time into buffer using %X format
             {
                printf("buffer = \"%s\"\n", buffer); //print buffer
             }
          }
       }
       return 0;
    }
    localtime() - http://msdn.microsoft.com/library/de..._localtime.asp
    strftime - http://msdn.microsoft.com/library/de..._.wcsftime.asp
    %X

    Time representation for current locale
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    12
    if ( strftime(buffer, sizeof buffer, "%X", mytime) ) //print the time into buffer using %X format

    pls explain this line more detail.

    why use &now- the memory address of now, why not use only now

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    please read the function description in the programmers reference
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    All I can do is help you alittle bit, mytime is undefined.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    12

    Smile

    Quote Originally Posted by vart
    please read the function description in the programmers reference

    Sorry about that, did'nt scroll down enough in the reference page.

    ther was very good exmple code in the bottom, much easier one.

    thank you!

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by kusal
    why use &now- the memory address of now, why not use only now
    Because the function takes a pointer, not a value.
    Quote Originally Posted by KoG Metalgod
    All I can do is help you alittle bit, mytime is undefined.
    Hm?
    Quote Originally Posted by kusal
    ther was very good exmple code in the bottom, much easier one.
    The ones I saw make assumptions. Are you aware of them? Or the other bad practices demonstrated?

    I took a (long overdue) stab at commenting it a bit...
    http://www.daniweb.com/code/printsnippet264.html
    (Thanks for the kick in the pants -- let me know how bad it sucks.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470

    Thumbs up

    Quote Originally Posted by Dave_Sinkula
    I took a (long overdue) stab at commenting it a bit...
    http://www.daniweb.com/code/printsnippet264.html
    (Thanks for the kick in the pants -- let me know how bad it sucks.)
    As always great work!
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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