How can I keep second with 2 digital?

K

kuoyang.hu

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
 
K

kuoyang.hu

(e-mail address removed)æ–¼ 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
 
G

guinness.tony

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);
 
K

kuoyang.hu

(e-mail address removed)æ–¼ 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
 
B

Barry Schwarz

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top