using system date calculate last 6 days in c language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhu3437
    New Member
    • Apr 2008
    • 13

    #16
    Code:
    int main()
    {
       printf("\n Date  6 days : %s\n", functionName());
       //Here u will get in full string format then u parce it and use the info what ever required.
    }// function last 6 days
    char *functionName()
    {
    char Date6[11]=NULL;
    time_t current,c6;
    struct tm      *tm,*now;
    current  = time(NULL);
    c6 = current - (6 * 3600 *24);
    now = localtime(&c6);
    
    //printf("Date is %02d/%02d/%02d\n", now->tm_mon+1, now->tm_mday,now->tm_year+1900);
    //printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min);
    snprintf(Date6, sizeof(Date6), "%02d/%02d/%02d", now->tm_mon+1, now->tm_mday, now->tm_year+1900);
    return Date6;
    }
    i write the above code is working fine and returning also MM/DD/YYYY same format so, here one more issue i get when printf is uncomment date is return if printf is comment date is not returning .

    i want comment the printf and return date to the main function.

    please help
    help........
    bye
    madhu
    Last edited by NeoPa; Jun 11 '08, 10:35 AM. Reason: [CODE] tags

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #17
      Please remember always to use the [ CODE ] tags provided when posting code.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #18
        Code:
        char *functionName()
        {
            char Date6[11]=NULL;
            time_t current,c6;
            struct tm      *tm,*now;
            current  = time(NULL);
            c6 = current - (6 * 3600 *24);
            now = localtime(&c6);
        
            //printf("Date is %02d/%02d/%02d\n", now->tm_mon+1, now->tm_mday,now->tm_year+1900);
            //printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min);
            snprintf(Date6, sizeof(Date6), "%02d/%02d/%02d", now->tm_mon+1, now->tm_mday, now->tm_year+1900);
            return Date6;
        }
        The problem in this code is that you are returning data that resides on the stack, that is you return Date6 but Date6 is declared as a local variable of functionName. As soon as function name exits Date6 is returned to the stack.

        This basically invokes undefined behaviour. Undefined behaviour is bad because once invoked your program will behave in a completely unpredictable manor. You have seen this, the function works with the printf statements in place and doesn't without them even though there is no logical reason why they should make a difference.

        Never return data that is located on the stack.

        You have 3 choices, the first, simple but somewhat more programming practice, is to make Date6 static (add the static keyword before it's declaration. This will then place Data6 in the global data segment and it will always exist so you wont have a problem returning it. However this method is not re-entrant and wont work on a multi-threaded system.

        The second is somewhat more complex, allocate memory from the heap (using malloc) inside functionName and return that data. Because it is allocated from the heap it will continue to exist after the function exits but the calling code will have to remember to free the data once it has finished with it.

        The third method is to pass a buffer (and a size of the buffer) into functionName from the calling code via a pointer and for functionName to put it's result directly into that buffer. The calling code then becomes responsible for allocating the memory (on stack, heap or globally) and freeing it if necessary once used.

        Personally I would used the third method, the first method doesn't work properly except in very simple circumstances, and I do not like allocating memory in a function at a lower level for release by a function at a higher level in a different part of the code. It tends to lead to memory leaks through thoughtless programming (i.e. forgetting to do the free) so that rules out method 2.


        And finally functionName is an extremely poor name for a function, the name of a function should give an idea of what it does.

        Comment

        • madhu3437
          New Member
          • Apr 2008
          • 13

          #19
          thanks very much ,i followed first one is working fine getting result into the main function....... ............... .........
          i want one more issue after result get in the main function how can hold the varibale...
          Code:
          main() 
          { 
          char     dateRequired [11]; 
          dateRequired= functionName()
          
          printf("date is %s" , dateRequired);
          }
          i try above code but result is not storing in the dateRequired variable....
          how can hold result the in the variable in the main function....

          please help........... .
          Regards,
          Madhu
          Last edited by NeoPa; Jun 12 '08, 09:51 AM. Reason: [CODE] tags and removed excessive quote

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #20
            Originally posted by madhu3437
            i try above code but result is not storing in the dateRequired variable....
            how can hold result the in the variable in the main function....
            I would be rather surprised if the code you posted compiles (it certainly doesn't for me) since dateRequired is not an l-value (simply put can not appear of the left hand side of an assignment).

            If you want the data in the calling function then I suggest you implement option 3 from my previous post that returns the data to the previous function.

            However you could try reading this.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #21
              Originally posted by NeoPa
              Please remember always to use the [ CODE ] tags provided when posting code.
              Madhu,

              This message is for you!

              If I have to say this a third time I will make the warning an official (and logged) one.

              ADMIN.

              Comment

              Working...