Thread: Function not returning date string

  1. #1
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75

    Function not returning date string

    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    #include<string.h>
    
    void date(char *);
    
    int main(void)
    {
        char dtstr[11];
        date(dtstr);
        puts(dtstr);
        return 0;
    }
    
    void date(char *)
    {
        int day, month, year;
        time_t now;
        time(&now);
        struct tm *local = localtime(&now);
        day = local->tm_mday;        
        month = local->tm_mon + 1;       
        year = local->tm_year + 1900;    
    
        char dtstr[11];
    
        char d[2];
        sprintf(d,"%d",day);
                
        char m[2];
        sprintf(m,"%d",month);
        
        char y[4];
        sprintf(y,"%d",year);
    
        strcat(dtstr,d);
        strcat(dtstr,"/");
        strcat(dtstr,m);
        strcat(dtstr,"/");
        strcat(dtstr,y);
    //    puts(dtstr);
    }
    Why is dtstr not available for printing in main()?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In your function implementation you need to "name" that parameter.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning A String From a Function
    By LarrySellers in forum C Programming
    Replies: 8
    Last Post: 09-01-2013, 08:40 AM
  2. Returning a string in a function
    By rpmischris in forum C Programming
    Replies: 2
    Last Post: 10-09-2012, 09:54 AM
  3. Return date (a string value) to function
    By CruelSoulz in forum C Programming
    Replies: 6
    Last Post: 06-25-2012, 03:39 AM
  4. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  5. function to return the date in a string
    By fborot in forum C Programming
    Replies: 10
    Last Post: 12-12-2005, 01:03 PM

Tags for this Thread