How can I keep second with 2 digital?

Discussion in 'C Programming' started by kuoyang.hu, Nov 27, 2013.

  1. kuoyang.hu

    kuoyang.hu Guest

    I have wrote a C program and wish the second with 2 digitals, how to do?

    Sample as follow:

    0 -> 00, 1 -> 01...9->09.
    ==============================
    My program as follow:
    #include <time.h>
    #include <stdio.h>

    int main(void)
    {
    time_t mytime;
    mytime = time(NULL);
    printf(ctime(&mytime));
    struct tm *t;
    time_t now;
    now = time ( NULL );
    t = localtime ( &now );
    int MySec = t->tm_sec;
    char *Init = "data";
    char buffer [30];
    char End [256];
    printf ( "The New %02d:%02d:%02d\n", t->tm_hour, t->tm_min, t->tm_sec );
    strcpy (End, Init);
    printf("Data = %s\n",End);
    itoa(MySec,buffer,10);
    strcat (End,buffer);
    printf("Data = %s\n",End);
    return 0;
    }

    ==================
    Executed on Windows with DOS command
    D:\tcc>getSecond.exe
    Wed Nov 27 17:05:01 2013
    The New 17:5:01
    Data = data
    Data = data1 <- I want the format is "Data = data1"

    Regard & thanks,
    Kuoyang
     
    kuoyang.hu, Nov 27, 2013
    #1
    1. Advertisements

  2. kuoyang.hu

    kuoyang.hu Guest

    æ–¼ 2013å¹´11月27日星期三UTC+8下åˆ5時18分58秒寫é“:
    Sorry, mistake, the correctly is
    Data = data1 <- I want the format is "Data = data01"

    Kuoyang
     
    kuoyang.hu, Nov 27, 2013
    #2
    1. Advertisements

  3. ^^^^^^^^^^^^^^^^^^^^^^
    Replace this line with

    snprintf(buffer, 10, "%02d", MySec);
     
    guinness.tony, Nov 27, 2013
    #3
  4. kuoyang.hu

    kuoyang.hu Guest

    æ–¼ 2013å¹´11月27日星期三UTC+8下åˆ5時18分58秒寫é“:
    Solved and thanks.
    I will study this "snprintf()".

    Thank & Regard,
    Kuoyang
     
    kuoyang.hu, Nov 27, 2013
    #4
  5. Your program is missing three function prototypes and at least two
    #include directives. You use the non-standard function itoa() when
    the standard sprintf() would work just as well. And you mistyped the
    output: the "New" line did not come from your program.

    When asking for help, it is to your advantage to provide us all the
    data and use cut/paste to avoid introducing unrelated errors.
     
    Barry Schwarz, Nov 27, 2013
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.