printf ("%*s", 4, "* * *" +4);

Source: Internet
Author: User

printf ("%*s", 4, "* * *" +4);
printf ("%*s/n", 6, "* * *" +0); Explain/collect Baidu a problem, a bit of use for yourself.

1. As explained first from%*s, the following format for the% printf is:
%[flags] [width] [. precision] [{h | l | ll | I | I32 | I64}]type

For detailed usage of these parameters, refer to MSDN:
HTTP://MSDN2.MICROSOFT.COM/EN-US/LIBRARY/56E442DC (vs.80). aspx

Here's what I'm talking about: the parameter [width] and the parameter [. Precision] If you use * to represent the two values is an input parameter, because in this case the%*s is not%.*s, so you can be sure that this * represents [width] ([flags] cannot be represented by *).

The [width] parameter represents a width, and if the input string is small and less than this width, it is populated with the value of the flags parameter, and if [flags] is empty, fill with a space.

In this example [width]=4, so if the string for%s is less than 4, then padding is preceded by a space.

2. "* * *" +4, what is that, in C the string "Hu-hu" represents a pointer address, "+4" is the pointer +4, so the result is a "*", you can try "* * *" +1, and "ABCDF" +4, the result is a "* * *", One is "f".
If you don't understand, try the following procedure:
Char *ss= "ABCDF";
printf ("%s,%s,%s,%s,%s", ss,ss+1,ss+2,ss+3,ss+4);

Summary: This printf represents: print a string, if the string is less than 4, padded with blanks, the fact that the address of the string is "* * *" +4, that is, the 4th "*" position, print the result "*"

=========================================
printf ("%*s/n", 6, "* * *" +0);
The output length is less than 6, padded with 6 spaces, and now your * has 5, so just fill a space.

printf ("%*s/n", 6, "********" +0);
This is not a single space, all is *, the length is insufficient to fill, and is missing how much

ch   =   "123456780123";  
    int   n   =   2,m   =  ;  
   //*.*  , the front is defined as the total width, and the number of outputs is defined in the rear.  
   //If the rear is smaller than the front, use a space to make up M-bit on the left.  
    printf ("%*.*s/n", m,n,ch)//Here Output "               " &nbs P
    printf ("%*.*s/n", n,m,ch);//Here Output "1234567890"   

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.