c language printf formatted output

#include <iostream>
#include<stdio.h>
#include <cstring>
using namespace std;


intmain ()
{

 char c_test[20] = {"1234567890"};
 double d_test = 12321;
 
 printf( " |%-15s|\n " ,c_test);   // Left-aligned, 15-bit length, not enough spaces |1234567890 | 
 printf( " |%15s|\n " ,c_test);    // Right-aligned, 15 Bit length, not enough to fill spaces | 1234567890| 
 printf( " |%015d|\n " ,d_test);   // Right alignment, 15-bit length, not enough to fill 0 |000000304558080| 
 printf( " |%-15.2f|\n " ,d_test); // Left-aligned, 15-bit length, with two decimal places, not enough spaces |12321.00 |

    return 0;
}

parameter

%a floating point number, hexadecimal number and p-notation (C99)
%A floating point number, hexadecimal number and p-notation (C99)
%c a character (char)
%C an ISO wide character           
%d signed decimal integer (int)
%f      single-precision floating point number (default float), decimal notation (
%.nf where n means accurate to n decimal places. Decimal counting)
%g automatically selects %f or %e according to different values.
%G automatically selects %f or %e according to the value.
%i signed decimal number (same as %d)
%o unsigned octal integer
%p pointer
%s      corresponds to string char* ( %S     corresponds to wide string WCAHR* ( %u unsigned decimal integer ( unsigned int )
%x  unsigned hexadecimal integer
using hexadecimal digit 0f %X unsigned hexadecimal integer using hexadecimal digit 0f
%% prints a hundred semicolon
 
①%: Indicates the start symbol of the format specification, which is indispensable.
②-: With- means left-aligned output, if omitted, it means right-aligned output.
③0: If there is 0, it means that the specified vacancy is filled with 0. If omitted, it means that the specified vacancy is not filled.
④m.n: m refers to the domain width, that is, the number of characters occupied by the corresponding output item on the output device. n refers to precision. The number of decimal places used to describe the output real number. When n is specified, the implied precision is n=6 bits.
⑤l or h:l refers to the long type for the integer type, and the double type for the real type. h is used to modify the format character of integer type to short type.
 
Format Characters Format characters are used to specify the data type and output format of the output item. ①d format: used to output decimal integers. There are the following usages: %d: output according to the actual length of the integer data. %md: m is the width of the specified output field. If the number of digits of the data is less than m, the left end will be filled with spaces; if it is greater than m, it will be output according to the actual number of digits. ②o format: output the integer in unsigned octal format. For long integers, you can use the "%lo" format to output. It is also possible to specify the field width to output in "%mo" format. Example: main() {
   int a = -1; printf("%d, %o", a, a); } Running result: -1,177777 Program parsing: -1 in the memory unit (stored in complement form) is (1111111111111111)2 , converted to octal as (177777)8. ③x format: output the integer in unsigned hexadecimal form. For long integers, it can be output in "%lx" format. You can also specify the field width to output in "%mx" format. ④u format: output integer in unsigned decimal form. For long integers, it can be output in "%lu" format. It is also possible to specify the field width to output in "%mu" format. ⑤c format: output one character. ⑥s format: used to output a string. There are several usages of %s: For example: printf("%s", "CHINA") outputs the "CHINA" string (excluding double quotes) %ms: The output string occupies m columns, if the length of the string itself is greater than m , then break through the limit of m, and output all the strings. If the length of the string is less than m, it will be left-padded with spaces. %-ms: If the length of the string is less than m, within the range of m columns, move the string to the left and fill with spaces to the right. %m.ns: The output occupies m columns, but only the left-end n characters in the string are taken. The n characters are output on the right side of column m, with spaces on the left. %-m.ns: The meanings of m and n are the same as above, and the n characters are output on the left side of the m column range, with spaces on the right. If n>m, the value of n is automatically taken, that is, the normal output of n characters is guaranteed. ⑦f format: used to output real numbers (including single and double precision) in decimal form. There are the following usages: %f: Without specifying the width, the integer part is all output and 6 decimal places are output. %m.nf: The output occupies a total of m columns, and there are n decimal places. If the width of the value is less than m, a space is added to the left. %-m.nf: The output occupies m columns in total, and there are n decimal places. If the width of the value is less than m, a space is added to the right. ⑧e format: output real numbers in exponential form. The following forms can be used: %e: The digital part (also known as the mantissa) outputs 6 decimal places, and the exponent part occupies 5 or 4 digits. %m.ne and %-m. ne: m, n and "-" characters have the same meaning as before. Here n refers to the number of decimal places in the digital part of the data, and m refers to the width occupied by the entire output data. ⑨g format: The shorter one of f format or e format is automatically selected for output, and meaningless zero is not output. ------------------------------------- Further explanation about the printf function: If you want to output characters "%", it should be represented by two consecutive % in the "format control" string, such as: printf("%f%%", 1.0/3); Output 0.333333%. ------------------------------------- For single precision numbers, use the %f formatter to output When , only the first 7 digits are significant digits, and the decimal is 6 digits. For double-precision numbers, when using the %lf format specifier to output, the first 16 digits are significant digits, and the decimal places are 6 digits.
 

#include <iostream>#include<stdio.h>#include <cstring>using namespace std;

int main(){
 char c_test[20] = {"1234567890"}; double d_test = 12321; printf("|%-15s |\n",c_test); //Left-aligned, 15-bit length, not enough spaces|1234567890 | printf("|%15s|\n",c_test); //Right-aligned, 15-bit length, not enough spaces| 1234567890| printf("|%015d|\n",d_test); //Right alignment, 15-bit length, not enough to fill 0 |000000304558080| printf("|%-15.2f|\n",d_test); //Left Alignment, 15 digits long, with two decimal places, not enough spaces | 12321.00 |
return 0;}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324795671&siteId=291194637