Answer to Question #214257 in C for Subi

Question #214257

Write a C program to print Hr:Min:Sec of clock using typedef


1
Expert's answer
2021-07-06T07:22:12-0400
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS