printf (), sprintf (), vprintf (), vsprintf (), signed | unsigned number

Source: Internet
Author: User
Tags clear screen lowercase mixed printf sprintf string format uppercase letter

http://sumsung753.blog.163.com/blog/static/14636450120112151092934/

First, printf ()

The advantage of the printf () function is that the output can be formatted.
printf () is a function that outputs a string, while print is a language structure that always returns True (1), and Echo is also a language structure that returns invalid.

total=12.4 If using echo, this is the case: echo "totalamountis total = 12.4 If using echo, this is the case: echo" All amount is all. ";
Output: Total amount is 12.4.
Return to printf for this expression:
printf ("Total amount is%s."; output: Totalamountis12.4.); Output: Total amount is 12.4. You can see that%s is replaced with the variable total.
In fact, the expression for printf () is this:
String sprintf (string format[,mixed format [, mixed args [, mixed $ ...])
Function inside the first parameter is a formatted string, which can have n% (conversion description), each conversion description will be in order by the re-format of the parameters replaced,% after at least one parameter –type (type code), indicating a certain meaning, see the parameter table:
Type code meaning
b interpreted as an integer and as a binary output
C interpreted as an integer and represents the output as a character (ASCII code)
D is interpreted as an integer and output as an integer
e interpreted as floating-point output
F interpreted as double precision and output as a floating-point number
o interpreted as an integer and output as an octal number
S is interpreted as a string and output to a string
U interpreted as unsigned integer output
G explanation automatic selection of appropriate representations
P interpreted as the value of the pointer
X is interpreted as an integer and is output as a hexadecimal number with a lowercase letter a-f
X is interpreted as an integer and is output as a hexadecimal number with an uppercase letter A-f

Example 1:
n=43951789; n = 43951789; u =-43951789;
$c = 65; ASCII-is ' A '

printf ("%%b = '%b ' \ n", n);//printf ("n"); %b = ' 10100111101010011010101101 ', binary output printf ("%%c = '%c ' \ n", c); %c = ' A ', as an ASCII character output, as with the Chr () function
printf ("%%d = '%d ' \ n", n);//printf ("n"); %d = ' 43951789 ', Integer printf ("%%e = '%e ' \ n", n); %e = ' 4.39518e+7 ', science notation
printf ("%%u = '%u ' \ n", n);//printf ("n"); %u = ' 43951789 ', unsigned number, positive * (Below is a signed, unsigned number) printf ("%%u = '%u ' \ n", u); %u = ' 4251015507 ', unsigned number, negative
printf ("%%f = '%f ' \ n", n);//printf ("n"); %f = ' 43951789.000000 ', double-precision floating-point number printf ("%%o = '%o ' \ n", n); %o = ' 247523255 ', octal
printf ("%%s = '%s ' \ n", n);//printf ("n"); %s = ' 43951789 ', string printf ("%%x = '%x ' \ n", n); %x = ' 29ea6ad ', hex, Small Letter
printf ("%%x = '%x ' \ n", $n); %x = ' 29ea6ad ', hex, uppercase

printf ("%%+d = '%+d ' \ n", n);//printf ("n"); %+d = ' +43951789 ', on a positive integer as + printf ("%%+d = '%+d ' \ n", u); %+d = '-43951789 ', on a negative integer +

Each conversion description adheres to the same format as follows:
%[' Padding_character][-][width][.precision]type
All conversion instructions start with%, and if you want to print a% symbol, you must use percent.
The parameter ' Padding_character (padding character) is optional and will be used to fill the variable until the specified width, and the function of the parameter is filled in front of the variable. The default padding character is a space, and if you specify 0 or a space, you do not need ' single quotation marks as a prefix, and other characters must be specified ' As a prefix.
Parameter-is optional, it is left-aligned, and the default is right-justified.
Parameter width refers to the length of the variable being replaced.
The parameter precision indicates the number of digits to display after the decimal point, starting with a decimal point.

Description
(1). You can insert a number between "%" and a letter to indicate the maximum 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 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 the string or the number of integers exceeds the width of the description, the output is based on its actual length.
⑤ floating point number, if the integer number of bits exceeds the width of the indicated integer digits, the actual integer digits will be output;
⑥ fractional digits exceed the indicated decimal width, the output is rounded by the width of the description.
⑦ If you want to add some 0 before the output value, 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 an output long integer
②%LF indicates 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 indicates output 7-bit integer left justified
②%-10s indicates output 10 characters aligns aligned

(4). Some special provisions of the character
①\n 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

Example 2:
year=2011

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.