When programming and debugging, log information is usually output to a file. At this time, you need to pay attention to the output alignment
In C language, it is common The There are two ways can achieve its output string or number aligned output
Tab character\t
Tab character\tOutput When , the output cursor will be moved to achieve alignment effect.
Therefore, you can add \t at the corresponding position of the output to achieve alignment.
But there is a disadvantage: it is required that the difference in the space occupied by the same column in each row cannot be too large.
(If the output is numeric information, you can copy the log information directly to the excel table, which can be a good way to count numerical information)
Add the occupied width control number
When using printf to format output, each control character can be written in the form of %nC, such as d, f, \, s, etc.
The corresponding part is the missing part Fill in spaces on the left to achieve a right-aligned effect;
If you want to fill in spaces on the right of the missing part, you only need to add a - symbol before the width character, such as %-12f, which achieves a left-aligned effect. .
Thank you everyone for reading, I hope you will benefit a lot.
This article is reproduced from: https://blog.csdn.net/chengzhilong94/article/details/54907141
Recommended tutorial: "C Language Tutorial"
The above is the detailed content of Detailed explanation of the two output alignment methods in C language (a must-read for beginners). For more information, please follow other related articles on the PHP Chinese website!