c++ format string description

C++ format strings are often used for formatting numeric output, string merging and conversion, and many other occasions.

  

1. Formatting specifiers

━━━━━━━━━━━━━━━━━━━━━━━━━━

   symbolic                  role

──────────────────────────

    %d              decimal signed integer

    %u              decimal unsigned integer

    %f               float

    %s              string

    %c              single character

The value of the     %p              pointer

    %e              floating point number in exponential form

    %x, %X       unsigned hexadecimal integer

    %0              unsigned octal integer

    %g              automatically selects the appropriate notation

━━━━━━━━━━━━━━━━━━━━━━━━━━

    illustrate:

    (1). A number can be inserted between "%" and a letter to indicate the maximum field width.

     For example: %3d   means to output a 3 -bit integer, which is not enough for 3 -bit right-alignment.

            %9.2f represents a floating -point number with an output field width of 9 , in which the decimal place is 2 and the integer place is 6.

                  The decimal point occupies one place, not enough to 9 right-aligned.

            %8s   means to output a string of 8 characters, not enough 8 characters are right-aligned.

    If the length of the string or the number of integer digits exceeds the specified field width, it will be output according to its actual length. However, for floating-point numbers, if the number of digits in the integer part exceeds the specified width of integer digits, it will be output according to the actual integer digits; if the number of digits in the fractional part exceeds the specified width of decimal digits, it will be rounded and output according to the specified width.

    In addition, if you want to add some 0s before the output value, you should add a 0 before the field width term .

    For example: %04d  means that when outputting a value less than 4 digits, 0 will be added in front to make the total width of 4 digits.

    If the output format of character or integer is represented by floating point number, the number after the decimal point represents the maximum width, and the number before the decimal point represents the minimum width.

    For example: %6.9s means to display a string whose length is not less than 6 and not more than 9 . If it is greater than 9,  the content after the 9th character will be deleted.

    (2). A lowercase letter l can be added between "%" and the letter, indicating that the output is a long number.

    For example: %ld   means output long integer

            %lf   means output double floating point number

    (3). The output can be controlled to be left-aligned or right-aligned, that is, adding a "-" sign between "%" and the letter indicates that the output is left-aligned, otherwise it is right-aligned.

    For example: %-7d  means to output a 7 -bit integer left justified

            %-10s means to output 10 characters left-aligned

 

2. Some special characters

━━━━━━━━━━━━━━━━━━━━━━━━━━

    character                           role

──────────────────────────

     \                   nNewline

     \fClear                   screen and form feed

     \                   rEnter

     \t                   Tab

     \xhh                 indicates that an ASCII code is represented by hexadecimal , where hh is 1 to 2 hexadecimal numbers

━━━━━━━━━━━━━━━━━━━━━━━━━━

 

         char c, s[20], *p;

          int a=1234, *i;

          float f=3.141592653589;

          double x=0.12345678987654321;

          p="How do you do";

          strcpy(s, "Hello, Comrade");

          *i=12;

          c='\x41';

          printf("a=%d\n", a); /* The result outputs a decimal integer a=1234*/

          printf("a=%6d\n", a); /* The result outputs a 6 -digit decimal number a= 1234*/

          printf("a=%06d\n", a); /* The result outputs a 6 -digit decimal number a=001234*/

          printf("a=%2d\n", a); /*a exceeds 2 digits, output a=1234 according to the actual value*/

          printf("*i=%4d\n", *i); /* output 4 decimal integer *i= 12*/

          printf("*i=%-4d\n", *i); /* Output left-aligned 4 -digit decimal integer*i=12*/

          printf("i=%p\n", i); /* Output address i=06E4*/

          printf("f=%f\n", f); /* output floating point number f=3.141593*/

          printf("f=6.4f\n", f); /* Output a 6 -digit floating point number with 4 decimal places f=3.1416*/

          printf("x=%lf\n", x); /* output long floating point number x=0.123457*/

          printf("x=%18.16lf\n", x);/* Output 18 - bit long floating point number x=0.1234567898765432*/

          printf("c=%c\n", c); /* output character c=A*/

          printf("c=%x\n", c); /* The ASCII code value of the output character c=41*/

          printf("s[]=%s\n", s); /* output array string s[]=Hello, Comrade*/

          printf("s[]=%6.9s\n", s);/* Output a string of up to 9 characters s[]=Hello,Co*/

          printf("s=%p\n", s); /* The address of the first character of the output array string s=FFBE*/

          printf("*p=%s\n", p); /* Output the pointer string p=How do you do*/

          printf("p=%p\n", p); /* output pointer value p=0194*/

 

    The address values ​​in the results above may vary on different computers.

Reprinted from: http://www.henryxu.com/post/1.html

Guess you like

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