C Language-printf function

Source: Internet
Author: User

printf function

1) Basic Concepts

The printf function is a library function provided by C's compiled system to output content to the output buffer

Specific stdio.h – header file for standard input (scanf) output (printf)

printf ("xxxx");/scanf ("%d", &a);

2) Use

Format: printf ("format-controlled string", variable list);

int a,b,c;

A = b = c = 10;

printf ("a =%d,b =%d,c =%d", a,b,c);

Note: How many placeholders are in the format-controlled string, and the number of variables that follow the list of variables

3) Problem with Format control (placeholder)

%d outputs an integer variable value (default is 10 binary)

%f real Type (single and double precision)

%lf

%c character type

%s String type

%x outputs the variable in 16 binary form

%o Output in 8-binary form

(1) When the output is added modifier

*)%MD m indicates the number of bits (domain width)

%5d

<m is a positive number;:

If the actual number of bits to be output is > m, output this number as-is

Actual Mantissa < M, left to fill empty (replenishment worth)

int a=2000;//number of digits =4

Prinf ("A=%3d", a);-->|2000|

Prinf ("a=%5d", a);-->| 2000|

<m is negative;:

If the actual number of bits to be output is > m, output this number as-is

Actual Mantissa < M, right fill empty

int a=2000;//number of digits =4

Prinf ("A=%3d", a);-->|2000|

Prinf ("a=%5d", a);-->|2000 |

* *)%0MD m represents the number of digits--this use can also

0 indicates the content of the complement

for (int i=0;i<81;i++) {

NSString *str = [NSString stringwithformat:@ "xxxx_%02d.jpg", I];

}

Xxxx_00.jpg

Xxxx_01.jpg

....

Xxxx_10.jpg

%f output a real-type data

The default is six digits after the decimal point (six digits after the decimal point, does not mean that the valid number is the latter six bit,

0.123456--six digits after the decimal point are valid digits

10.123456--only 5 digits after the decimal point is valid, the 6th bit of output may be garbled, 10.123450,10.12345x)

(1)%m.nf

m represents the total number of digits, if the number of bits (m is positive, left padding blank, otherwise right padding)

M is can be omitted%.2f reserved decimal 2 digits (common)

n indicates the number of digits after the decimal point

(2)%f accuracy problem

Data of type float has 7 valid digits by default. However, after the decimal point, the default six-bit, that is,->3.000000;

Double doubles the default number of 16 digits that are valid. However, after the decimal point, the default six-bit, that is,->3.000000;

C Language-printf function

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.