printf output format for various languages that vary from c

Source: Internet
Author: User
Tags function prototype print print sprintf

printf is often used in C, PHP, and the shell, especially in PHP, where there are several variants of printf: print, printf, sprintf, vprintf, vsprintf in these languages, printf is basically formatted output string, and basically is on the basis of C, and the following is a record in PHP, although there are so many in PHP, but the difference between them is relatively small 1, print print "he Llo php "or php (" Hello php ") print is just a language structure, not a function, similar to echo, so you can output without using () and directly" "the difference between print and Echo is that print has a return value and always returns 1.  2, printf () This is an output function, so instead of using the function prototype in the same way as above:int printf (string $format [, Mixed $args [, mixed $ ...])3, sprintf () As with printf, it is important that the difference function prototype between the two:string sprintf (String $format [, Mixed $args [, mixed $ ...])Difference: printf () outputs the formatted string to standard output, returns the length of the output string sprintf () returns the strings, does not output the string to the browser 4,            The difference between vprintf () and vsprintf () is that they have only two parameters: an output format, an array of variables.    The above printf () and sprintf () in addition to the first fixed output format, the following parameter variables are determined according to the variable data that needs to be output in the string. Now, the emphasis is on the output format (like the C language, and so is the shell), which is the first parameter prototype for these functions:%[Padding-character][-][width][. Precision]Type black is required, [] in the expression optional (according to actual demand)% represents the start of the conversion description, note to output%, the way of Padding-character: fill character, default is a space padding, if you use a space or 0 you can not add "'", fill the other characters need to be preceded by a single quotation mark-the alignment, the default left alignment,-then the right alignment, width length, this length is: the whole The length of the number part +.+ is said to be: 3.4 is 3 lengths, 30.4 is 4, that is, this block is calculated by character. Precision decimal parts.                   Represents a decimal point, precision represents a few digits after the decimal point.                For example: Above width is ten, Padding-character is 0, parameter is: 30.4,precision is 2, then type is f output what? 0000030.40

1<?PHP2     /*3 * create_wangxb_201501164 * Print related knowledge points5      * 6      */7     Echo"* * * print is just a language structure, not a function, similar to echo; so you can output ***<br/> without using () and directly" ";8     $res 0=Print"Hello php";9     Echo"<br/>";Ten     $res 1=Print("Hello php"); One     Echo"The difference between <br/>*********** print and Echo is that print has a return value and always returns 1 ****************<br/>"; A     Echo"Print return value: res0=$res 0<br/>print () return value: res1=$res 1<br/> Echo is not a return value of "; -     Echo"<br/>*********** definition variable: num=3.4 ***********<br/>"; -     $num= 30.4; the     Echo $num; -     Echo"<br/>"; -     $res 2=printf(' This number is%.2f ',$num); -     Echo"<br/>******** printf () outputs the formatted string to the standard output, returning the length of the output string ******<br/>"; +     EchoThe printf () return value res2:$res 2<br/> "; -     Echo"<br/>******** sprintf () returns a string that does not output the string to the browser ******<br/>"; +     $res 3=sprintf(' This number is%.2f ',$num); A     EchoThe sprintf () return value Res3 (see Clearly, here we are outputting the return value):$res 3<br/> "; at      -     Echo"<br/> such as width above, padding-character is 0, parameter is: 30.4,precision is 2, then type is f<br/>"; -     printf("Num is%010.2f",$num); -  -     Echo"<br/>padding-character is * when, that is, non-whitespace and 0 o'clock <br/>"; -     printf("num is% ' *10.2f",$num); in  -     Echo<br/> second item [-] When right-aligned <br/>; to     printf("num is% ' *-10.2f",$num); +  -     Echo<br/> the third item width, that is, the length of the change, note to calculate the decimal point of the character <br/> "; the     printf("num is% ' *-8.2f",$num); *  $     Echo<br/> fourth precision, that is, the change in the length of the decimal point <br/>;Panax Notoginseng     printf("num is% ' *-8.3f",$num); -  the     Echo"<br/> fifth type, that is, change the output d<br/>"; +     printf("num is% ' *-8d",$num); A?>

Output:

  

printf output format for various languages that vary from c

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.