If no precision is specified for %f
argument of printf
, it is using 6 decimal digits.
Also, for printf
there is no need to use %lf
, float
arguments are always promoted to double
when passed (Why does printf() promote a float to a double?). But usually, a printf
implementation will ignore the l
prefix.
For a proper print alignment, in this case you can use:
printf("%4.1f ", mat[i][j]);
// Here a space is added after printing the number.
// "%5.1f" could also be used for alignment (difference: the space will be prefixed),
// but then if the number will have 3 or more integer digits, it will be displayed
// "concatenated" with the previous one. Using a space prevents this
Using this, there will be 4 positions allocated to print each number, from which one position will be used by the decimal point, and another one for the decimal places (1 in this case). Two positions are remaining for the integer part (if it doesn't fit in these 2 positions, more will be used and may broke the alignment.
If running in a default Windows terminal: Even if using this alignment, there will not be enough room to print a matrix row on a single line, and 2 lines will be used. This is because by default, Windows terminal line width is 80 characters. Increasing it to 100 or more will fix the problem. This can be changed using the Properties options, or even programatically, as described here:
How can I change the width of a Windows console window?