C language: printf format string

Source: Internet
Author: User

PrintfFormat String :% [Mark] [[*] [Data width] [.Precision]Type

 

+

-

0

#

Minimum Width

Precision

Type

display symbol

left alignment

( right-aligned by default )

data field 0

(right aligned)

8 hexadecimal addition 0

16 base Addition 0x

10-digit integer

or * NO.

example: . 2

-

Y

-

-

Y

-

C

-

Y

-

-

Y

-

S

Y

Y

Y

-

Y

-

d , I

-

Y

Y

-

Y

-

U

-

Y

Y

Y

Y

-

O

-

Y

Y

Y

Y

-

x

Y

Y

Y

-

Y

Y

F,E,G,A

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-This option is not required for this type.

As shown in the preceding table, only the data width and alignment are supported by all types. The right alignment is performed by default when the width is specified.

Only when the minimum width is specified can we use the-, 0 Sign. In this case, alignment and fill make sense. In fact, we only need to specify the minimum width, if the width of the data is insufficient, the data will be filled with blank characters or filled in the front of the data or in the back of the data (depending on the alignment) 0 indicates filling in front of the data with 0. This is to make the Data Alignment look nice. It must be noted that although many types support left alignment and fill with 0, but in fact, these two options are conflicted. The left alignment cannot be filled with 0, and the left alignment cannot be filled with 0. The left alignment means filling with blank spaces after the data.

 

Examples

 

"% 12D" The data width must be at least 12 characters wide. If the data is less than 12 characters long, the data is right aligned.

"% +-12D" 12D indicates the same as above. The difference is that the print symbol and data are left aligned.

"% # X" Data 3A is printed as 0x3a

"%. 4f" Data retains the precision of four digits after the decimal point

In fact, the format string of printf is the same as that of sprintf.

 

Char buffer [100];

Sprintf (buffer, "% 10d", 12 );

Printf ("the string is % d bytes \ n", strlen (buffer ));

Output the string is 10 bytes

 

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.