Thread: Trying to print day

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16

    Trying to print day

    Here the code:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main (void) {
    time_t ctime = time(NULL);
    char c [10];
    
    const char format[] ="%Y %m %d";
    struct tm *today = localtime(&ctime);
    
    strftime(c, sizeof(c), format, today);
    puts(c);
    
    
        return 0;
    }
    It almost works, but the output is a little bit stange. I got the year month and day but in the end it just outputs some random stuff, for example 2007 06 19&\220wF@\340

    What I'm doing wrong?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You have not enough space to put the null-char in the string
    So your result is not properly null-terminated
    Use bigger buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main (void) 
    {
        time_t ctime = time(NULL);
        char c [20];
    
        const char format[] ="&#37;Y %m %d";
        struct tm *today = localtime(&ctime);
    
        strftime(c, sizeof(c), format, today);
        puts(c);
        
        getchar();
        return 0;
    }
    /* my outout
    2007 06 19
    */
    ssharish2005

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16
    oh, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. day number in yaer
    By azsquall in forum C++ Programming
    Replies: 6
    Last Post: 05-24-2008, 02:44 AM
  2. Printing weekday
    By sworc66 in forum C Programming
    Replies: 12
    Last Post: 09-13-2002, 07:03 AM
  3. Replies: 1
    Last Post: 07-31-2002, 11:35 AM
  4. send data to printer and print on paper
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 07-22-2002, 05:19 AM
  5. one fine day...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-15-2001, 06:45 PM