printf Usage---printf output fixed-length characters

Source: Internet
Author: User

Need to print a fixed-length character

It is generally possible to do this:

    • void Str_print (const char* str, unsigned int str_len)
    • {
    • int i=0;
    • for (; i < str_len; i++)
    • printf ("%c", Str[i]);
    • printf ("\ n");
    • }


But a single line of code can also be done:

printf ("%.*s\n", Str_len, str);

printf General usage:

Ref:http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html

Decimal points. After "*" indicates the number of output bits, the specific data from the parameter table
In the printf format string, constants related to width control and precision control can be converted to variables by using a "*" instead of that constant, and then supplying the variable to "*" later.

Similarly, the decimal points. You can also add a * to the user, and you need to enter a bit width value instead, which represents the width of the output character.

#include <cstdio> #include <iostream>int main () {Char *s ="This is test example";int A, B; printf"%.*s\n",Ten, s);//The constant 10 here is for the * number, you can also use a variable to control the width printf (%*.*s\n ", 20, 10, s); // constant 20 controls the width of the output, or it can be controlled with a variable // printf ( "%*.*s\n", A, B, s); // output:-----This is Te std:: Cin. get (); Std::cin.ignore (); // pause program execution       

The output is:

This is Te

----------This is te//-represents a space

15 10//input

-----This is TE

> Note: The general form of printf is printf ("Format control string", output list), and the format control string is: [flag] [Output min width] [. precision] [length] type.

The items in square brackets [] are optional. The meanings of the items are described as follows:
1) Type: The type character is used to represent the type of the output data , and its format character and meaning are shown in the following table:
D output signed integers in decimal form (positive numbers do not output symbols)
o Output unsigned integers in eight binary form (not output prefix 0)
X,x output unsigned integers in 16 binary form (does not output prefix Ox)
U output unsigned integers in decimal form
F output single, double-precision real numbers in decimal form
E,e output single, double-precision real numbers in exponential form
G,g output single, double-precision real numbers with shorter output widths in%f or%e
C Output Single character
S output string
2) Flag: The flag character is-, +, #, space four kinds , the meaning of the following table is shown:
-Results left-aligned, right-hand blanks
+ Output symbol (plus or minus)
The space output value is a positive-time space, minus the negative.
# No effect on C,s,d,u class, O class, prefix o on output, x class, prefix 0x on output, and decimal point for e,g,f class when the result has a decimal number
3) Output minimum width: A decimal integer representing the minimum number of digits of the output . If the actual number of digits is greater than the defined width, the actual number of digits is output, or if the actual number of digits is less than the defined width, a space or 0 is provided.
4) Accuracy : Precision format character with "." Begins with a decimal integer followed by. The meaning of this item is: If the output number, then the number of decimal places, if the output is a character, the number of output characters, if the actual number of digits is greater than the defined number of precision, the section is truncated.
5) Length : Length format character is h,l Two, h means output by short integer, l means output by long integer quantity.

printf Usage---printf output fixed-length characters

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.