How can I keep second with 2 digital?

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

  1. 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
    , Nov 27, 2013
    #1
    1. Advertising

  2. Guest

    æ–¼ 2013å¹´11月27日星期三UTC+8下åˆ5時18分58秒寫é“:
    > 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


    Sorry, mistake, the correctly is
    Data = data1 <- I want the format is "Data = data01"

    Kuoyang
    , Nov 27, 2013
    #2
    1. Advertising

  3. Guest

    On Wednesday, 27 November 2013 09:18:58 UTC, wrote:
    > 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);

    ^^^^^^^^^^^^^^^^^^^^^^
    Replace this line with

    snprintf(buffer, 10, "%02d", MySec);

    > 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
    , Nov 27, 2013
    #3
  4. Guest

    æ–¼ 2013å¹´11月27日星期三UTC+8下åˆ5時18分58秒寫é“:
    > 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


    Solved and thanks.
    I will study this "snprintf()".

    Thank & Regard,
    Kuoyang
    , Nov 27, 2013
    #4
  5. On Wed, 27 Nov 2013 01:18:58 -0800 (PST), wrote:

    >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"


    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.

    --
    Remove del for email
    Barry Schwarz, Nov 27, 2013
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Janne Naukkarinen
    Replies:
    1
    Views:
    371
    Jack Klein
    Jan 15, 2004
  2. Janne Naukkarinen
    Replies:
    0
    Views:
    370
    Janne Naukkarinen
    Jan 14, 2004
  3. SutterCreek
    Replies:
    1
    Views:
    375
    Peter Flynn
    Dec 24, 2006
  4. yelipolok
    Replies:
    4
    Views:
    253
    John W. Krahn
    Jan 27, 2010
  5. hisan
    Replies:
    1
    Views:
    1,315
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page