Search code examples
cprintffigures

How to always make a print of 4 figures in C


I am working on a project for university. One of the small things that make me lose points is that my answer is correct but it doesn't have four figure number.

For example say I print my answer which is 17 and on other test files it could change to 6. Is there a way I can ensure that it would always print either

0017 -> testfile_1
0006 -> testfile_2
0123 -> testfile_3

so it always has four digits no matter what the solution is? I made lots of test cases to check if the value is a single number or multiple to work it out but is there a simple way to do this so it automatically adds the zeros?


Solution

  • Try to use

    printf( "%04d", myValue);
    

    04 will make sure that your myValue will always have, at least, 4 digits.