Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C Time Function

Hi I want to printf the dateApp function

For example I expected : 19:04
But the result is : 19:4

How to modify the function, so the function always return 2 digits, Thankyou!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void dateApp(){
    char line[500] = "\xb3";
    time_t currentTime;
    time(&currentTime);
    struct tm *myTime = localtime(&currentTime);
    printf("Date : %i/%i/%i       \xb3 Time : %i:%i %44s", myTime ->tm_mday, myTime->tm_mon + 1, myTime->tm_year + 1900, myTime->tm_hour, myTime->tm_min, line );
}

>Solution :

You need to add a field width of 2 to each %i if you want it to always print at least 2 characters, along with the 0 flag to tell it to pad with zeros on the left.

printf("Date : %02i/%02i/%i       \xb3 Time : %i:%02i %44s", ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading