I am trying to create a graphical table in C by using a 2D int array. It all works well but the problem is that if the integer's length's are different, the table doesn't look good anymore. Here is an example:
Good
[ 198 ] [ 2 ] [34313]
[ 4 ] [ 54 ] [ 6 ]
[ 7 ] [ 888 ] [ 9 ]
Not good
[198] [2] [34313]
[4] [54] [6]
[7] [888] [9]
What I come up with was to get the length of the biggest int
and then use %-Xd
for spacing but I don't know how to use a variable at the place of the X. My thought was to replace X by %d
, i.e. printf(" [%-%dd]", maxLength, mat[i][j]);
but all I get is [%dd].
Does anyone know of a way to solve this problem?