Use %2d
in your printf
instead of %d
.
Question
How do I make the output in w way so every column would be aligned? My current formatting is:
printf("Posting #%d, Timestamp: %d, Light: %d, Temperature: %d, Humidity: %d\n", i, valArr[i][j], valArr[i][j+1], valArr[i][j+2], valArr[i][j+3]);
Solution
OTHER TIPS
As pointed out in the first answer to the question, %2d
will print an integer two characters wide, %3d
will print a decimal integers, at least 3 characters wide.
There's also other formatting options in printf
for floating point variables and octal/hex, for example:
%6f
will print as floating point, at least 6 characters wide.
%.2f
will print as floating point, 2 characters after the point.
You can omit either one from the statement, it's still valid. Simply the width will not be constrained.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow