C printf Format Summary

Source: Internet
Author: User
Tags clear screen

Turn from: Click to open the link

1. The calling format is printf ("< format string >", < Parameter table >);
The format string consists of two parts: part of the normal character, and the characters are output as is; The other part is formatting the specified character, starting with "%" followed by one or more specified characters to determine the format of the output content. Parameter table is a series of parameters that need to be output, the number must be the same as the number of output parameters described in the format string, the parameters are separated by "," and the order one by one corresponds, otherwise there will be unexpected errors.

2. Formatting characters

%d decimal signed integer
%u decimal unsigned integer
%f floating-point numbers
%s string
%c single character
The value of the%p pointer
Floating point numbers in the form of%e exponent
%x, int without symbol in hexadecimal notation
%0 An integer that is unsigned as octal
%g automatic selection of appropriate notation
Description
(1). You can insert a number between "%" and a letter to indicate the maximum field width. For example:%3d represents an output 3-bit integer and is not 3-bit right-aligned. %9.2F represents a floating-point number with an output field width of 9, where the decimal digit is 2, the integer digit is 6, the dot is one digit, and the 9-bit right alignment is not enough. %8s represents a 8-character string that is not 8 words Fu Yi aligned. If the length of the string, or the integer number of digits exceeds the field width of the description, it will be output at its actual length. But for floating point number, if the integer partial digit exceeds the indicated integer digit width, it will be output according to the actual integer digit; 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 to the presence width. For example,%04d indicates that when outputting a value less than 4 digits, the front complement of 0 will make the total width 4 bits. If you use floating-point numbers to represent the output format for characters or integers, the digits after the decimal point represent the maximum width, and the digits before the decimal point represent the minimum width. For example,%6.9s represents a string that is not less than 6 and not greater than 9. If greater than 9, the contents of the 9th character will be deleted.
(2). You can add a lowercase letter L between "%" and a letter to indicate the length of the output. For example,%LD represents the output long integer, and%LF represents the output double floating-point number.
(3). You can control the output left or right alignment, that is, add a "-" sign between "%" and the letter to indicate that the output is left-aligned or right-aligned. For example:%-7d represents the output of a 7-bit integer left-aligned,%-10s indicates that the output is 10 characters left-aligns.


3. Some special specified characters
\ n Line Change
\f Clear screen and change page
\ r Carriage Return
\ t Tab character
\XHH represents an ASCII code with a 16-in representation,
where HH is 1 to 2 16 in number

int a=1234;
printf ("a=%d\n", a); a=1234
printf ("a=%2d\n", a); a=1234 more than 2 digits, 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 digits, front 0
printf ("a=%-6d\n", a); a=1234 '-' left-aligned


int* i=&a;
printf ("i=%p\n", I); I=0012ff44 the value of the output pointer, which is the address

float m=8888.8888; Float single-precision floating-point number valid digits are 6-bit or 7-bit, depending on different floating-point numbers
float m1=8888.8888f; followed by F or F, 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 double-precision floating-point number valid digits are 15 digits
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
n1=8888888888.888889
%F's default output scale is 6 digits, whether l or not.
The%f specifier for/*printf does output both a float and a double. Based on the "Default parameter elevation" rule (in the case of a function such as printf
In a variable argument list, this rule applies regardless of whether there are prototypes in the scope. The float is promoted to double. So printf () will only see
Double-precision number. Strictly speaking,%lf is undefined under printf, but many systems may accept it. To ensure portability, you should insist on using%f. */

printf ("m4=%4.2f\n", m); Width total 4 digits, decimal two digits, one decimal point, one integer, here the integer exceeds the width of the specified, according to the actual integer digit output
printf ("m5=%9.6f\n", m); //
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.