The printf () function for C language notes

Source: Internet
Author: User

First look at the example:
printf("This line has only controlling strings.\n");
' printf ("I am%d years old and%f inch tall", 21, 132);

  • Parameters
    The parameters of printf () are divided into two parts, the control string and the parameter list, separated by commas, and separated by commas between the parameters. Where the control string is a string constant, if no other parameters are required, the text inside the double quotation marks will be printed to the screen. In layman's words, what is inside the double quotes, what is the output, including the escape character.
    However, in this way, no matter what we want to print, we can only write it manually inside the double quotation marks, so that we can't automate printing. For example, how to output the value of a variable in a program? You can't change the value of a variable once. Let's rewrite the printf () function manually? That goes against the original intention of writing the program.
    So there has to be a way for the function to print the value of the variable, however, it is not possible to write the variable name directly inside the double quotes, because the variable name becomes part of the string constant-- 双引号的内部的字符将被视为普通文本。
    Thus, a special character '% ' is introduced into the control string, which is escaped by combining certain characters with the following character, which is no longer treated as normal text, but has other uses. For example,%d in the example above, which indicates that the position is to print a decimal number instead of%d itself, the actual value is provided by the corresponding variable or constant following the string. % is called a placeholder, which means to occupy a fixed position before adding content to it; d is the format specifier indicating which format the corresponding parameter is printed in. %d is called a format placeholder or conversion specifier.
    As you can see, a parameter can be a variable or a constant, or it can be an expression. Note, however, that the parameters must match the number of conversion specifiers in the control string and the order of the arguments, and that the type of each parameter matches the corresponding conversion specifier, such as floating-point numbers should not be printed with%d. the use of various conversion specifiers is described below.

  • Conversion specifier
    What does the above-mentioned format conversion mean? We want to know that any data in the computer is stored in binary form, the variable or constant corresponding to the storage space may be a string of binary numbers: 01000001, then it is understood as a hexadecimal number or in the form of 16 binary printing is 0x41 , printed in decimal form is 65, printed in ASCII characters is the capital letter ' A '. * Therefore, the essence of format conversion is translation, the binary number is translated into the corresponding form. The various format specifiers are the rules for specifying translations. *
    Here we describe each of these format specifiers.

    • Integer type

      • %d or%i: signed decimal integers, their effect is the same, as follows:
        printf("this is %d and %i, same.\n", 10, 10);
        Output: This is ten and same.

      • %u: unsigned decimal integer:
        printf("this is %d and %u.\n", -65, -65);
        Output: This is-65 and 4294967231.
        As we can see, 65 this number is stored in the same binary form, but there is a variation in the interpretation, and the principle is described earlier.

      • %x or%x: hexadecimal unsigned integer:
        printf("this is %d and %x.\n", -65, -65);
        printf("this is %d and %X.\n", -65, -65);
        Output: This is-65 and FFFFFFBF.
        This is-65 and FFFFFFBF.

      • %o : Octal unsigned integer:
        printf("this is %d and %o.\n", -65, -65);

    • Floating-point type

      • %f: Floating-point number for decimal notation:
        printf ("This is%f\n", 12.62543219);
        Output: This is 12.625432
        as you can see,%f rounds the number after seven digits after the decimal point, which means its precision is 6.
      • %e or%e:e or e-notation floating-point number:
        printf ("This is%e\n", 0.625);
        printf ("This is%e\n", 9000876.54);
        Output: This was 6.250000e-01
        This is 9.000877e+06
        you can see that%e or%e can only retain six digits after the decimal point.

      • %g or%g: At this point, printf represents a floating-point number as an e-notation and retains only six valid digits, and then observes the exponent, which automatically chooses%f if the exponent is between -4~+5, or%e or%e (which seems to be more complex, Let's judge for yourselves):
        printf ("This is%g and%g\n", 0.000089, 0.00089);
        printf ("This is%G and%g\n", 900876.44, 9000876.44);
        Output: This is 8.9e-05 and 0.00089
        The is 900876 and 9.00088E+06

      • % A or%a: The floating-point number of the hexadecimal p or p notation (I don't know what the hell it is):
        printf ("This is%a and%a\n", 3.75, -3.75);
        Output: This is 0x1.ep+1 and-0x1. Ep+1
    • Other types

      • %c: one character:
        printf("this is %c \n", 66);
        Output: This is B
      • %s: string:
        printf("this is %s \n", "string");
        Output: This is string
      • %p: pointer:
        printf("this is %p \n", "string");
        Output: This is 0x8048576
      • %: percent of the semicolon itself, because a single% has been "requisitioned" by the compiler, escaped into placeholders, so to print% itself had to use two% to replace:
        printf("this is %% \n");
        Output: This is%
  • Conversion description modifier
    Various modifiers can be inserted between the% and format specifiers to further modify the final print results, such as the width of the print, the precision, the symbol, and so on.

    • ' Number ': used to specify the minimum width of the print field, and if the width does not fit the result, the limit is automatically exceeded to use a wider field:
      printf("this is %6d \n", 65);
      Output: This is 65

    • '. Number ': Look at mate objects, if floating-point format specifiers such as%f, specify the precision of floating-point numbers, that is, the number of digits to the right of the decimal point, or%s, the maximum number of characters to print, and the integer format that specifies the minimum digits of the printed number (the precision of integers). ), not enough words with 0 to make a leading fill; if there is no number after the decimal point, the default is 0, which becomes an integer:
      printf("this is /%.2f/\n", 65.3);
      printf("this is /%.f/\n", 65.3);
      printf("this is /%.3d/\n", 65);
      Output: This is/65.30/
      This is/65/
      This is/065/

    • '-': minus, used to specify the alignment, from the above result we can see that the default alignment is right-aligned, and the use of '-', indicating to the left alignment:
      printf("this is /%6d/\n", 65);
      printf("this is /%-6d/\n", 65);
      Output: This is/65/
      This IS/65/

    • ' + ': Prints the number of symbols, whether the number is exactly negative. In general, only with the use of symbolic format specifier, such as%+d,%+f, but%+u,%+c and so on will be error:
      printf("this is %+c \n", 65);
      Output: This is +65

    • ': This is a space, the function and ' + ' are similar, the difference is to use a space instead of the printed ' + '; if used with ' + ', because of its lower priority, the effect will be overwritten by ' + ':
      printf("this is /% -6d/\n", 65);
      Output: this IS/65/
      You can see that there is a space in front of 65.

    • ' 0 ': For all number formats, fill in the blanks with a leading 0 instead of a space; if integer precision or '-' is specified, the flag is invalidated:
      printf("this is /%05d/\n", 65);

    • ' # ': used primarily to print octal or hexadecimal preamble symbols, occasionally with floating-point types:
      printf("this is /%#5x/\n", 65);
      printf("this is /%#5o/\n", 65);
      Output: This is/0x41/
      This is/0101/

    • Various modifiers for different numeric type conversions, which are used in conjunction with numeric type specifiers:
      H: denotes a short or unsigned short, such as%hd
      HH: Represents a signed char or unsigned char, such as%hhu%HHX
      L: Represents a long or unsigned long, such as%lo,%ld
      Ll:long long or unsigned long long,%llx
      L:long Double
      T: can only be used with integer types, indicating the type of difference between pointers, note that it is not the pointer type itself,%TD
      Z: can only be combined with an integer type, which represents a size_t value, which is the type of sizeof return value,%ZD

The printf () function for C language notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.