The C-language printf () function is detailed

Source: Internet
Author: User
Tags truncated

The printf function is called the format output function, and the last letter F of its keyword is the meaning of format. the function is to display the specified data to the display screen according to the user-specified format. We have used this function many times in the previous example.

General form of printf function call

The printf function is a standard library function whose function prototype is in the header file "Stdio.h". However, as a special case, it is not required to include the Stdio.h file before using the printf function. the general form of the printf function call is:
printf ("Format control string", Output table column)
Where the format control string is used to specify the output format. The format control string can consist of both a format string and a non-formatted string. A format string is a string that begins with a% followed by a variety of format characters to indicate the type, form, length, scale, etc. of the output data. Such as:

    • "%d" means output by decimal integer;
    • "%ld" indicates the output by decimal length integer;
    • "%c" means output by character type and so on.


The non-formatted string is output as a hint in the display. Each output item is given in the output table column, which requires that the format string and each output item should correspond to one by one on both the quantity and the type.

Example of "Example 4-3" printf function

#include <stdio.h>int main (void) {    int a=88,b=89;    printf ("%d%d\n", A, b);    printf ("%d,%d\n", A, b);    printf ("%c,%c\n", A, b);    printf ("a=%d,b=%d", A, b);    return 0;}

In this example, the values of a and B are output four times, but the output is not the same due to the different format control strings. The output statement in line 3rd controls the string with a space (not a format character) between the two format string%d, so there is a space between the A and B values of the output. The printf statement format in line 4th controls the non-formatted character comma added to the string, so a comma is added between the values A and B of the output. The format string for line 5th requires a, B value to be output by character type. The non-format string is added in line 6th in order to prompt the output.

format string

The general form of the format string in Turbo C is:
Flag [Output min. width] [. Accuracy] [Length] type.
The items in square brackets [] are optional.

The meanings of the items are described below.

1) Type
The type character is used to represent the type of the output data, and its format character and meaning are shown in the following table:

format Characters meaning
D Output signed integers in decimal form (positive numbers do not output symbols)
O Output unsigned integers in eight binary form (not output prefix 0)
X,x Output unsigned integers in 16 binary form (does not output prefix ox)
U Output unsigned integers in decimal form
F Output single, double-precision real numbers in decimal form
E,e Output single, double-precision real numbers in exponential form
G,g Output single, double-precision real numbers in a short output width of%f or%e
C Output a single character
S Output string

2) logo
The flag characters are--, +, #, and four of the spaces, and their meanings are shown in the following table:

logo meaning
- The result is left-aligned and the right-fill grid
+ Output symbol (plus or minus)
Space The output value is a positive-time space, minus the negative.
# No influence on C, S, D, U class;
For the O class, the output is prefixed with o;
For the X class, the output is prefixed with 0x;
For E, G, F class, the decimal point is given when the result has a decimal number.


3) Output Minimum width
A decimal integer that represents the minimum number of digits for the output. If the actual number of digits is greater than the defined width, the actual number of digits is output, or if the actual number of digits is less than the defined width, a space or 0 is provided.

4) Accuracy
Precision format characters with "." Begins with a decimal integer followed by. The meaning of this item is: If the output number, then the number of decimal places, if the output is a character, the number of output characters, if the actual number of digits is greater than the defined number of precision, the section is truncated.

5) Length
The length format character is H, l Two, h means output by a short integer, l means output by a long integer quantity.

"Example 4-4"

#include <stdio.h>int main (void) {int a=15;    Long float b=123.1234567;    The original definition of B is float b=123.1234567;   By the reader @ Deng feedback, the author found overflow, in front added a long double c=12345678.1234567;   Char d= ' P ';   printf ("a=%d\n", a);  printf ("A (%%d) =%d, A (%%5d) =%5d, A (%%o) =%o, A (%%x) =%x\n\n", a,a,a,a);   Percent can output% printf ("a=%f\n", b);   printf ("B (%%f) =%f, B (%%LF) =%LF, B (%%5.4LF) =%5.4LF, B (%%e) =%e\n\n", b,b,b,b);   printf ("c=%f\n", c);   printf ("C (%%LF) =%LF, C (%%f) =%f, C (%%8.4LF) =%8.4lf\n\n", c,c,c);   printf ("d=%c\n", D);   printf ("D (%%c) =%c, D (%%8c) =%8c\n", d,d); return 0;}


In this example:

    • The 11th line outputs the value of the integer variable A in four formats, where "%5d" requires an output width of 5, and a value of 15 has two digits so it is three spaces.
    • Line 14th outputs the value of the real amount B in four formats. The output of the "%f" and "%LF" formats is the same, stating that the "L" character has no effect on the "F" type. "%5.4LF" Specifies the output width of 5, the precision is 4, because the actual length of more than 5 should be based on the actual number of outputs, the number of decimal digits more than 4 parts are truncated.
    • The 17th line outputs a double-precision real number, and "%8.4LF" is truncated to more than 4 bits because the specified precision is 4 bits.
    • The 20th line outputs the character D, where "%8c" specifies an output width of 8 so that 7 spaces are added before the output character p.


One more thing to note when using the printf function is the order of evaluation in the output table column. Different compilation systems are not necessarily the same, either from left to right or from right to left. Turbo c is done from right to left. take a look at the following two examples.

"Example 4-5" in a printf () output

#include <stdio.h>int main (void) {    int i=8;    printf ("The Raw value:i=%d\n", i);    printf ("++i=%d \n++i=%d \n--i=%d \n--i=%d\n", ++i,++i,--i,--i);    return 0;}

Operation Result:
The Raw value:i=8
++i=8
++i=7
--i=6
--i=7

"Example 4-6" is output in multiple printf ()

#include <stdio.h>int main (void) {    int i=8;    printf ("The Raw value:i=%d\n", i);    printf ("++i=%d\n", ++i);    printf ("++i=%d\n", ++i);    printf ("--i=%d\n", i.);    printf ("--i=%d\n", i.);    return 0;}

Operation Result:
The Raw value:i=8
++i=9
++i=10
--i=9
--i=8

The difference between the two programs is output with a printf statement and multiple printf statements. But from the results can be seen is different. Why would the results be different? This is because the printf function evaluates the values in the output table in the order from right to left.

It is important to note, though, that the order of evaluation is from right to left, but the output order is still left to right, so the result is the above output.

Note: After the reader @ Dragon Totem Hao Little feedback, we found that the original example in the VC6.0 under the complexity of the calculation process, can not be intuitive to explain the order of evaluation from right to left. The above code is modified, and the date of modification is 2014-07-05.

Here is the old code and analysis:

"Example 4-5"
Main () {
int i=8;
printf ("%d\n%d\n%d\n%d\n%d\n%d\n", ++i,--i,i++,i--,-i++,-i--);
}

"Example 4-6"
Main () {
int i=8;
printf ("%d\n", ++i);
printf ("%d\n",--i);
printf ("%d\n", i++);
printf ("%d\n", i--);
printf ("%d\n",-i++);
printf ("%d\n",-i--);
}

The difference between the two programs is output with a printf statement and multiple printf statements. But from the results can be seen is different. Why would the results be different? This is because the printf function evaluates the values in the output table in the order from right to left. In the first example, the last item "-i--" is evaluated first, the result is-8, then I is 7 after 1. Again the "-i++" item is worth 7, then I 1 after the increment is 8. Then the "i--" item is worth 8, then I again 1 after the minus 7. Then the "i++" item was 7, then I was increased by 1 after 8. Then the "-I" Item, I first self-minus 1 after the output, the output value is 7. Finally, the first item in the Output table column is "++i", when I increment 1, I output 8.

The C-language printf () function is detailed

Related Article

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.