Standard output of printf in C language

Source: Internet
Author: User

1. Call format printf ("< format string >", < Parameter table >);
Where the formatted string consists of two parts: some are normal characters, and the characters are output as-is; The other part is the formatting of the specified character, starting with "%", followed by one or more specified characters, used to determine the output content format. The parameter table is a series of parameters that need to be output, the number must be as many as the number of output parameters as indicated by the formatted string, the parameters are separated by "," and the order one by one corresponds, otherwise unexpected errors will occur.

2. Formatting characters

%d decimal signed integer
%u decimal unsigned integer
%f floating Point
%s string
%c single character
%p the value of the pointer
Floating-point numbers in the form of%e indices
%x,%x unsigned integer in hexadecimal notation
%0 unsigned integer represented in octal
%g automatic selection of appropriate representations
Description
(1). You can insert a number between "%" and a letter to indicate the maximum field width. For example:%3d represents the output of 3-bit integers, not enough 3-bit right-justified. The%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2, the integer digit is 6, the decimal point is one, and not enough 9-bit right-aligned. The%8s represents the output of a 8-character string, which is not aligns aligned to 8 characters. If the length of a string, or the number of integers exceeds the width of the description, it is output at its actual length. But for floating-point numbers, if the integer number of bits exceeds the width of the indicated integer digits, it will be output according to the actual integer digits; If the fractional number of digits exceeds the indicated decimal width, the output is rounded by the width of the description. In addition, if you want to add some 0 before the output value, you should add a 0 before the field width. For example,%04d means that when you output a value that is less than 4 bits, it will be preceded by 0 to make its total width 4 bits. If you use floating-point numbers to represent the output format of a character or integer, 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 indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If it is greater than 9, the contents of the 9th character will be deleted.
(2). You can add a lowercase letter L between "%" and the letter, indicating that the output is a long form. For example,%ld indicates the output of a long integer, and%LF represents the output double floating-point number.
(3). You can control the output left or right alignment, that is, "%" and the letter by adding a "-" sign to indicate that the output is left-justified, otherwise it is right-justified. For example:%-7d represents the output 7-bit integer left-aligned,%-10s represents the output of 10 characters aligns aligned.


3. Some special provisions of the character
\ nthe line break
\f Clear screen and change page
\ r Enter
\ t Tab character
\XHH represents an ASCII code with 16-in notation,
where HH is 1 to 2 x 16 binary number

int a=1234;    printf ("a=%d\n", a);   a=1234 printf ("a=%2d\n", a);   a=1234 more than 2 bits, according to the actual output printf ("a=%6d\n", a);  A= 1234 less than 6 bits, right-aligned printf ("a=%06d\n", a);  a=001234 less than 6 bits, preceded by 0 printf ("a=%-6d\n", a);    a=1234 '-' left justified int* i=&a;    printf ("i=%p\n", I);     I=0012ff44 the value of the output pointer, that is, the address float m=8888.8888;    Float single-precision floating point number is 6 or 7 bits, according to the different floating-point numbers will have different float m1=8888.8888f; Add F or F at the back, compile warning: Truncation from ' const double ' to ' float '//compiler default floating-point number is double float m2=8888.888f      ;    Double n=8888.8888;  Double n1=8888888888.88888888; Double dual-precision floating-point number is 15 bits of printf ("m=%f\n m1=%f\n m2=%f\n n=%lf\n n1=%f\n", m,m1,m2,n,n1);                                                                     m=8888.888672//m1=8888.888672                                                               m2=8888.887695                                                          n=8888.888800     The default output scale of the n1=8888888888.888889//%f is 6 digits, whether or not L/*PR The%f specifier of intf does not only output float type but also output double type. The float type is promoted to type double, depending on the default parameter promotion rule (in the mutable argument list for a function such as printf, regardless of whether there is a prototype in the scope). So printf () will only see double-precision numbers. Strictly speaking,%lf is undefined under printf, but many systems may accept it. To ensure portability, stick with%f.   */printf ("m4=%4.2f\n", m);   Width Total 4 bits, decimal two bits, decimal place, integer one, here the integer exceeds the width of the specified, according to the actual integer digits output printf ("m5=%9.6f\n", m);   The number of floating-point numbers is less than 6 bits, right-aligned printf ("m6=%9.2f\n", m);    The integer part is less than 6 bits, right-aligned, fractional part more than 2 bits, rounded char c[20]= "hello,world!";        printf ("c=%s\n", c);  printf ("c=%6.9s\n", c); C=hello,wor 6.9s indicates the output of a string with a length of not less than 6 and not greater than 9. If it is greater than 9, the contents of the 9th character will be deleted.

Standard output of printf in C language

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.