0

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!

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 );
}
Nico A.L
  • 25
  • 1
  • 8

1 Answers1

1

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", ...
dbush
  • 205,898
  • 23
  • 218
  • 273