I want the program to return 4 digits even if the number is less than 4 digits long.
for example:
1 -> 0001
I tried:
printf("%4d",number);
but it only returns three spaces then the number one, it doesn't add the 0s before.
I want the program to return 4 digits even if the number is less than 4 digits long.
for example:
1 -> 0001
I tried:
printf("%4d",number);
but it only returns three spaces then the number one, it doesn't add the 0s before.
By default, white spaces are used for padding, you should add 0
to the format specifier to make the padding character 0
s.
printf("%04d",number);
In addition to Yu Hao's answer, you can also specify the digit count dynamically:
printf("%0*d", digit_count, number);