第一个C代码电子钟

#include
#include
#include
void delay(int t);
int main(void)
{
while(1)
{
// clock_t start_clock;
time_t current=time(NULL);
struct tm *ptr;
char date_time[21];
int hour;
char am_or_pm;

system("cls"); 
//print date and time in dafault format
puts(ctime(¤t));
//print date and ,using strftime to format
strftime(date_time,sizeof(date_time),
"%m-%d-%Y %I:%M%p\n",localtime(¤t));
puts(date_time);
//print date and time ,using printf to format
ptr=localtime(¤t);
hour=ptr->tm_hour;
if(hour<=11)
am_or_pm='a';
else
{
hour-=12;
am_or_pm='p';
}
if(hour==0)
hour=12;
printf("%.2d-%.2d-%.2d %2d:%.2d%c\n",ptr->tm_mon+1,ptr->tm_mday,
ptr->tm_year+1900,hour,ptr->tm_min,am_or_pm);
//start_clock=clock();
delay(1000);
// printf("Processor time used:%g sec.\n",(clock()-start_clock)/(double)CLOCKS_PER_SEC);
}
return 0;
}


void delay(int i)
{
int k;
for(;i>0;i--)
for(k=0;k<373000;k++);
}

你可能感兴趣的:(第一个C代码电子钟)