Printf ("% * s", 4, "*****" + 4); Explanation

Source: Internet
Author: User
Printf ("% * s", 4, "*****" + 4); Explanation

Printf ("% * s/n", 6, "******" + 0); explains // adding a Baidu to favorites and is useful to you!

1. Start with % * s. The format behind the % of printf is:
% [Flags] [width] [. Precision] [{H | L | ll | I | i32 | i64}] Type

For detailed usage of these parameters, see msdn:
Http://msdn2.microsoft.com/en-us/library/56e442dc (vs.80). aspx

Here I will only focus on: [width] and [. precision] If "*" is used, the two values are an input parameter, because in this example, % * s is not %. * s, so it is certain that this * represents [width] ([flags] cannot be expressed ).

The [width] parameter indicates a width. If the input string is small and cannot be of this width, it is filled with the value of the [flags] parameter. If [flags] is empty, fill in with spaces.

In this example, [width] = 4. Therefore, if the % s string is smaller than 4, spaces will be filled before.

2. "*****" + 4, indicating what, in C string "********" indicates a pointer address, "******" + 4 indicates this pointer + 4, so the result is a "*". You can try "******" + 1, and "abcdf" + 4. The result is "*****" and the result is "F ".
If you do not understand it, try the following program:
Char * Ss = "abcdf ";
Printf ("% s, % s", SS, SS + 1, SS + 2, SS + 3, SS + 4 );

Conclusion: This printf indicates printing a string. If there are less than 4 strings, fill them with spaces. The actual address of the string is "*****" + 4, that is, print the result at the location of 4th "*"

========================================================== =
Printf ("% * s/n", 6, "*****" + 0 );
The output length is less than 6 and 6 spaces are filled in. Now you have 5 x spaces. Therefore, you only need to fill in one space.

Printf ("% * s/n", 6, "*********" + 0 );
This space does not exist, all of which are *. If the length is insufficient, the space is filled.

 

 

 

 

Ch = "123456780123 ";
Int n = 2, M = 10;
// *. *, The front side * defines the total width, and the back side defines the number of outputs.
// If the back side is smaller than the front side, use spaces to fill up M places on the left side.
Printf ("% *. * s/n", M, N, CH); // output "12" here"
Printf ("% *. * s/n", n, m, CH); // output "1234567890" here"

Printf ("% * s/n", 6, "******" + 0); explains // adding a Baidu to favorites and is useful to you!

1. Start with % * s. The format behind the % of printf is:
% [Flags] [width] [. Precision] [{H | L | ll | I | i32 | i64}] Type

For detailed usage of these parameters, see msdn:
Http://msdn2.microsoft.com/en-us/library/56e442dc (vs.80). aspx

Here I will only focus on: [width] and [. precision] If "*" is used, the two values are an input parameter, because in this example, % * s is not %. * s, so it is certain that this * represents [width] ([flags] cannot be expressed ).

The [width] parameter indicates a width. If the input string is small and cannot be of this width, it is filled with the value of the [flags] parameter. If [flags] is empty, fill in with spaces.

In this example, [width] = 4. Therefore, if the % s string is smaller than 4, spaces will be filled before.

2. "*****" + 4, indicating what, in C string "********" indicates a pointer address, "******" + 4 indicates this pointer + 4, so the result is a "*". You can try "******" + 1, and "abcdf" + 4. The result is "*****" and the result is "F ".
If you do not understand it, try the following program:
Char * Ss = "abcdf ";
Printf ("% s, % s", SS, SS + 1, SS + 2, SS + 3, SS + 4 );

Conclusion: This printf indicates printing a string. If there are less than 4 strings, fill them with spaces. The actual address of the string is "*****" + 4, that is, print the result at the location of 4th "*"

========================================================== =
Printf ("% * s/n", 6, "*****" + 0 );
The output length is less than 6 and 6 spaces are filled in. Now you have 5 x spaces. Therefore, you only need to fill in one space.

Printf ("% * s/n", 6, "*********" + 0 );
This space does not exist, all of which are *. If the length is insufficient, the space is filled.

 

 

 

 

Ch = "123456780123 ";
Int n = 2, M = 10;
// *. *, The front side * defines the total width, and the back side defines the number of outputs.
// If the back side is smaller than the front side, use spaces to fill up M places on the left side.
Printf ("% *. * s/n", M, N, CH); // output "12" here"
Printf ("% *. * s/n", n, m, CH); // output "1234567890" here"

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.