speaking of printf() function, students who write code must be very familiar with it. This is a standard printing function in C language, which is often used when debugging code or information output.
The conversion of printf function is described as follows:
Conversion description modifier
Tags in printf()
The following is a practical example to illustrate the functions of modifiers and tags.
For example, now collect the voltage value of external equipment through three sensors, and print the collected data in real time through the serial port. The relevant codes printed are as follows:
printf( "\r\n ch max min rms ave \r\n" ); adc_all_value(0, &chAll_value); printf("%d %d %d %d %d\r\n", 0, chAll_value.max, chAll_value.min, chAll_value.rms, chAll_value.ave); printf("%d %f %f %f %f\r\n", 0, chAll_value.max * 3.3 / 4096, chAll_value.min * 3.3 / 4096, chAll_value.rms * 3.3 / 4096, chAll_value.ave * 3.3 / 4096); adc_all_value( 1, &ch1_value ); printf( "%d %d %d %d %d\r\n", 1, ch1_value.max, ch1_value.min, ch1_value.rms, ch1_value.ave ); printf( "%d %f %f %f %f\r\n", 1, ch1_value.max * 3.3 / 4096, ch1_value.min * 3.3 / 4096, ch1_value.rms * 3.3 / 4096, ch1_value.ave * 3.3 / 4096 ); adc_all_value( 2, &ch2_value ); printf( "%d %d %d %d %d\r\n", 2, ch2_value.max, ch2_value.min, ch2_value.rms, ch2_value.ave ); printf( "%d %f %f %f %f\r\n", 2, ch2_value.max * 3.3 / 4096, ch2_value.min * 3.3 / 4096, ch2_value.rms * 3.3 / 4096, ch2_value.ave * 3.3 / 4096 );
The results output through the serial port are as follows:
The maximum value, minimum value, effective value and average value of the sampling channel are displayed respectively. But the format of the printed data looks laborious. It is hoped that the data can be aligned according to each column when printing. The first thought is to separate these data by spaces.
The code is modified as follows:
printf( "\r\n ch \t\tmax \t\t min \t\t rms \t\t ave \r\n" ); adc_all_value(0, &chAll_value); printf("%d \t\t %d \t\t %d \t\t %d \t\t %d\r\n", 0, chAll_value.max, chAll_value.min, chAll_value.rms, chAll_value.ave); printf("%d\t\t%f \t %f \t %f \t %f\r\n", 0, chAll_value.max * 3.3 / 4096, chAll_value.min * 3.3 / 4096, chAll_value.rms * 3.3 / 4096, chAll_value.ave * 3.3 / 4096); adc_all_value( 1, &ch1_value ); printf( "%d \t\t %d \t\t %d \t\t %d \t\t %d\r\n", 1, ch1_value.max, ch1_value.min, ch1_value.rms, ch1_value.ave ); printf( "%d\t\t%f \t %f \t %f \t %f\r\n", 1, ch1_value.max * 3.3 / 4096, ch1_value.min * 3.3 / 4096, ch1_value.rms * 3.3 / 4096, ch1_value.ave * 3.3 / 4096 ); adc_all_value( 2, &ch2_value ); printf( "%d \t\t %d \t\t %d \t\t %d \t\t %d\r\n", 2, ch2_value.max, ch2_value.min, ch2_value.rms, ch2_value.ave ); printf( "%d\t\t%f \t %f \t %f \t %f\r\n", 2, ch2_value.max * 3.3 / 4096, ch2_value.min * 3.3 / 4096, ch2_value.rms * 3.3 / 4096, ch2_value.ave * 3.3 / 4096 );
Add a tab between the data so that the output data is separated by spaces.
In this way, the printed data looks neat, but when debugging the code, because the length of each data output is different, it needs to be debugged many times to determine how many spaces there are between the two columns of data, and the code looks messy. At this point, you can use the modifiers and tags of the printf function to format the output data.
The code is modified as follows:
char *s1="ch"; char *s2="max"; char *s3="min"; char *s4="rms"; char *s5="ave"; printf( "\r\n%4s %12s %12s %12s %12s\r\n",s1,s2,s3,s4,s5 ); adc_all_value(0, &chAll_value); printf("%4d %12d %12d %12d %12d\r\n", 0, chAll_value.max, chAll_value.min, chAll_value.rms, chAll_value.ave); printf("%4d %12.6f %12.6f %12.6f %12.6f\r\n", 0, chAll_value.max * 3.3 / 4096, chAll_value.min * 3.3 / 4096, chAll_value.rms * 3.3 / 4096, chAll_value.ave * 3.3 / 4096); adc_all_value( 1, &ch1_value ); printf( "%4d %12d %12d %12d %12d\r\n", 1, ch1_value.max, ch1_value.min, ch1_value.rms, ch1_value.ave ); printf( "%4d %12.6f %12.6f %12.6f %12.6f\r\n", 1, ch1_value.max * 3.3 / 4096, ch1_value.min * 3.3 / 4096, ch1_value.rms * 3.3 / 4096, ch1_value.ave * 3.3 / 4096 ); adc_all_value( 2, &ch2_value ); printf( "%4d %12d %12d %12d %12d\r\n", 2, ch2_value.max, ch2_value.min, ch2_value.rms, ch2_value.ave ); printf( "%4d %12.6f %12.6f %12.6f %12.6f\r\n", 2, ch2_value.max * 3.3 / 4096, ch2_value.min * 3.3 / 4096, ch2_value.rms * 3.3 / 4096, ch2_value.ave * 3.3 / 4096 );
At this time, the data output effect is as follows:
At this time, the data looks clearer. The data is sampled in the right alignment by default.
The number modifier is mainly used here
Convert the name of each column into a string, and then print the character instead of directly outputting the string, so that the length of the output character can be limited by numbers. For example, the character string "ch" is set to occupy 4 characters. In this way, when the character length is less than 4 characters, it will be filled with spaces. Then set the length of other characters to 12 bits, and the empty position will be filled with spaces, which is the same as printing spaces directly.
For integer numbers, the number modifier also sets the length of bits occupied. If the length of bits is not enough, it will be replaced by a space. For decimals, the number before the decimal point represents the overall length occupied by the decimal point, and the number after the decimal point represents the number of decimal places reserved. For example, ""% 12.6f "means that the floating-point number occupies 12 digits as a whole and the decimal part retains 6 digits. In other words, the integer part occupies 6 digits and the decimal part also occupies 6 digits.
The printed characters are right aligned by default. If you want to align left, just add a "-" sign after% directly. Here, the print data is modified to be left aligned, and 2 decimal places are reserved.
The modification code is as follows:
char *s1="ch"; char *s2="max"; char *s3="min"; char *s4="rms"; char *s5="ave"; printf( "\r\n%-4s %-12s %-12s %-12s %-12s\r\n",s1,s2,s3,s4,s5 ); adc_all_value(0, &chAll_value); printf("%-4d %-12d %-12d %-12d %-12d\r\n", 0, chAll_value.max, chAll_value.min, chAll_value.rms, chAll_value.ave); printf("%-4d %-12.2f %-12.2f %-12.2f %-12.2f\r\n", 0, chAll_value.max * 3.3 / 4096, chAll_value.min * 3.3 / 4096, chAll_value.rms * 3.3 / 4096, chAll_value.ave * 3.3 / 4096); adc_all_value( 1, &ch1_value ); printf( "%-4d %-12d %-12d %-12d %-12d\r\n", 1, ch1_value.max, ch1_value.min, ch1_value.rms, ch1_value.ave ); printf( "%-4d %-12.2f %-12.2f %-12.2f %-12.2f\r\n", 1, ch1_value.max * 3.3 / 4096, ch1_value.min * 3.3 / 4096, ch1_value.rms * 3.3 / 4096, ch1_value.ave * 3.3 / 4096 ); adc_all_value( 2, &ch2_value ); printf( "%-4d %-12d %-12d %-12d %-12d\r\n", 2, ch2_value.max, ch2_value.min, ch2_value.rms, ch2_value.ave ); printf( "%-4d %-12.2f %-12.2f %-12.2f %-12.2f\r\n", 2, ch2_value.max * 3.3 / 4096, ch2_value.min * 3.3 / 4096, ch2_value.rms * 3.3 / 4096, ch2_value.ave * 3.3 / 4096 );
The printing results are as follows:
As you can see, the adjusted format looks clearer.
By using the modifiers and tags of printf, the program looks more concise and the output format is more beautiful.