For C programming. How do i get numbers to be displayed as 00, 01, 02, 03, instead of 0, 1, 2, 3. I just need 0 before the number until 10.
i know when your doing decimals you can do "%.2f" etc. but what about in reverse for integers?
here is what I am using...**
printf("Please enter the hours: ");
scanf ("%d",&hour);
printf("Please enter the minutes: ");
scanf ("%d",&minute);
printf("Please enter the seconds: ");
scanf ("%d",&second);
printf("%d : %d : %d\n", hour, minute, second);
}
I need the numbers to display as 00 : 00 : 00
??
You need to use
%02d
if you want leading zeroes padded to two spaces:See for example the following complete program:
which outputs:
Keep in mind that the
%02d
means two characters minimum width so it would output 123 as123
. That shouldn't be a problem if your values are valid hours, minutes and seconds, but it's worth keeping in mind because many inexperienced coders seem to make the mistake that 2 is somehow the minimum and maximum length.Use the format:
%02d
instead. The0
means to pad the field using zeros and the2
means that the field is two characters wide, so for any numbers that take less than 2 characters to display, it will be padded with a 0.