Write a C program to print Hr:Min:Sec of clock using typedef
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <time.h>//for get curent time
#include <malloc.h>
//Implement struct Time
typedef struct Time
{
int hr;//Hour
int min;//Minut
int sec;//Second
}Time;
//
typedef struct tm tm;
Time* curTime()
{
time_t cur = time(0);//Curent time second
tm* now = localtime(&cur);
Time* cT=(Time*)malloc(sizeof(Time));//Alloc memory
cT->hr = now->tm_hour;//Curent hour
cT->min = now->tm_min;//Minut
cT->sec = now->tm_sec;
return cT;
}
//function display format time hh:mm:ss
void PrintTime(Time* t)
{
printf("\t\t%i%i:%i%i:%i%i", t->hr / 10, t->hr % 10, t->min / 10, t->min % 10, t->sec / 10, t->sec % 10);
}
int main()
{
Time* curT = curTime();
PrintTime(curT);
free(curT);
}
Comments
Leave a comment