Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to format output function by printf in C language

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to format the output function of printf in C language. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Int printf (const char * format, [argument])

The output format of the format parameter, which is defined as:

% [flags] [width] [.perc] [F | N | h | l] type

Specify the method of data output, as follows:

1.type has the following meanings:

D signed decimal integer

I signed decimal integer

O signed octal integer

U unsigned decimal integer

X unsigned hexadecimal number, expressed in lowercase abcdef

X unsigned hexadecimal number, expressed in uppercase ABCDEF

Fhand f floating point number

Floating-point numbers in scientific format

G uses the total number of digits in the% f and% e representations to represent the shortest floating point number G in the same format as g, but as an exponent.

C single character

S string

% shows the percent sign itself

P displays a pointer, and the near pointer is expressed as: XXXX

The far pointer is expressed as: XXXX:YYYY

The n-connected parameter should be a pointer in which the number of written characters is stored.

2.flags specifies the output format. The values and meanings are as follows:

No right alignment, left filled with 0 and spaces

-align left, fill in blanks on the right

+ add a symbol before the number + or-

A space displays symbols only for negative numbers

# there is no effect when type=c,s,d,i,u

When type=o,x,X, add'0x', "0x" and "0X" before the value respectively.

When type=e,E,f, always use the decimal point

When type=g,G, the decimal point is always displayed except that the value is 0. 3.width is used to control the width of the display value. The value and meaning are as follows. The width is at least n bits, not enough to be filled with spaces.

0n (nasty 1pm 2pm 3...) The width is at least n bits, not enough to fill the * grid with 0 on the left.

In the formula list, the next parameter is still used to control the number of digits after the decimal point. The values and meanings are as follows:

No default precision display

0 when type=d,i,o,u,x, no effect

When type=e,E,f, the decimal point is not displayed

N (nasty 1pm 2pm 3...) The maximum number of decimal places represented when type=e,E,f

Type= other, indicating the maximum width of the display. *

In the format list, the next parameter is width.

5.F | N | h | l indicates whether the pointer is a remote pointer or whether an integer is a long integer

F far pointer

N near pointer

H short integer or single precision floating point number

L long integer or double precision floating point number

1. General format

Printf (format control, output table columns)

For example: printf ("itemized% djinchen% c\ n", iMagnech)

Description:

(1) "format control" is a string enclosed by double apostrophes, also known as "conversion control string". It includes two kinds of information:

① format description: composed of "%" and format characters, its function is to convert the output data to the specified format output.

② normal characters, that is, characters that need to be output as is.

(2) "output table column" is some data that needs to be output, which can be an expression.

(3) the general form of printf function can be expressed as

Printf (parameter 1, parameter 2,... , parameter n)

The function is to output parameter 2 ~ parameter n according to the format given by parameter 1

two。 Format characters (9)

(1) d (or I) format character. Used to output decimal integers, there are several uses:

① d, output by the actual length of the integer data.

② md,m is the width of the specified output field. If the number of digits of the data is less than m, the left end is filled with a space, and if it is greater than m, it is output by the actual number of digits.

③% ld (% mld is also available) to output long integer data.

For example: long axiom 123456

Printf ("% ld", a)

(2) the o format character, which outputs integers as octal numbers. Format:% O.P.% MOM.% lo.J.% MLO.

(3) the x (or X) format character that outputs integers as hexadecimal numbers. Format:% x _ Magi% mx _ Magi% lx _ Magi% MLX.

(4) the u format character, which is used to output unsigned data, that is, unsigned numbers, as decimal numbers. Format:% umel% mumel% lu.

See: li4-3.cplash * output of unsigned data * /

(5) the c format character, which is used to output a character. Format:% cMagne% MC is fine.

(6) s format character, which is used to output a string. The format:% sforce% msrech% Mui msjie% m.nsjie% murm.ns is fine.

See: li4-5.c / * string output * /

(7) f format character, which is used to output real numbers (including single and double precision) in decimal form. Format:% fjorn% m.nfpenny% Murm.nf is fine.

Note: the significant digits of single-precision real numbers are generally 7 digits, and double precision is 16 digits.

Refer to: li4-6.cplash * significant digits when outputting single-precision real numbers * /

Li4-7.cplink * significant digits when outputting double-precision real numbers * /

Li4-8.cplink * specify the number of decimal places when outputting real numbers * /

(8) the e (or E) format character, which outputs real numbers exponentially. Format:% edepartment% m.nejie% Murm.ne is all right.

(9) g (or G) format character, which is used to output real numbers, which automatically selects f format or e format according to the size of the value (choose the one with a smaller width when the output is selected).

3. Description

(1) except for X, E, G (represented by uppercase letters), other format characters must be in lowercase letters.

(2) the "format control" string can contain escaped characters.

(3) if you want to output the character "%", you should use two consecutive% in the format Control string, such as:

Printf ("% f%%", 1.0 take 3)

(4) the format character table is shown in the following table.

Table 4.1 printf format characters

Format character specification

DBI outputs integers in signed decimal form (positive numbers do not output symbols)

O output integers in octal unsigned form (no leading character 0)

XQuery X outputs integers in hexadecimal unsigned form (without the leading character 0x), lowercase when using x to output a hexadecimal number, and uppercase when using X.

U outputs integers in unsigned decimal form

C output in character form, only one character is output

S output string

F outputs single and double precision numbers in decimal form, implicitly outputting 6 decimal places

EQuery E outputs real numbers in exponential form

GMagne G chooses a format with shorter output width in% f or% e format, and does not output meaningless 0.

Table 4.2 additional format description characters for printf

Character

Description

Letter l

For long integer integers that can be added before the format characters d, o, x, u

M (represents a positive integer)

Minimum width of data

N (represents a positive integer)

For a real number, it indicates the output of n decimal places; for a string, it indicates the number of characters intercepted.

-

The output number or character is left in the field.

Program example:

# include

Int main ()

{

Printf ("Hello,world\ n")

While (1)

}

# include

Int main ()

{

Int I = 1, j = 2

Printf ("% d% d\ n", iMagnej)

While (1)

}

Printf command

Use

Write formatted output.

Grammar.

Printf Format [Argument...]

Description

The printf command converts, formats, and writes Argument parameters to standard output. The Argument parameter is formatted under the control of the Format parameter. The formatted output line cannot exceed the LINE_MAX byte length.

The following environment variables affect the execution of the printf command:

LANG determines the locale used by the locale catalog when LC_ALL and the corresponding environment variable (starting with LC_) do not specify the locale.

LC_ALL determines the locale used to override any locale catalog value set by LANG or any other LC_ environment variable.

LC_CTYPE determines the locale in which the order of text byte data is interpreted as characters; for example, a single byte corresponds to a parameter of a multibyte character.

LC_MESSAGES determines the language used to write messages.

LC_NUMERIC determines the locale in which numbers are formatted. This environment variable affects the format of numbers written using e, E, f, g, and G conversion characters.

The Format parameter is a string that contains three object types:

* unformatted characters are copied to the output stream.

* convert specifications, each of which results in the retrieval of 0 or more items in the value parameter list.

* the following escape sequence. When copied to the output stream, these sequences cause their related operations to be displayed on devices with this function:

\\ backslash

\ a warning

\ b backspace

\ f Page change

\ nWrap

\ r enter

\ t jump box

\ v Vertical tabbing

\ ddd ddd is a 1, 2, or 3 octal number. These escape sequences are displayed as bytes with numeric values specified by octal numbers.

The Argument parameter is a list of one or more strings that are written to standard output under the control of the Format parameter.

The Format parameter is often reused to satisfy the Argument parameter if necessary. Any additional c or s conversion specifications will be evaluated as if an empty string Argument is provided; other additional conversion specifications will be evaluated as if they provided 0 Argument. Here the Format parameter does not contain the conversion specification and only the Argument parameter appears, and the result is uncertain.

The transformation specification in each Format parameter has the following order of syntax:

1.% (percent sign).

two。 Zero or more options to modify the meaning of the conversion specification. Option characters and their meanings are:

-the conversion result is aligned to the left in the field.

+ symbol conversion results often begin with a symbol (+ or -).

Spaces if the first character of the symbol conversion is not a symbol, the result will be prefixed with a space. If both the space and + option characters are displayed, the space option character is ignored.

# this option specifies that the value is converted to an alternate format. For c, d, I, u, and s conversions, the option has no effect. For o conversion, it increases precision to force the first number of the result to be a, 0 (zero). For x and X conversions, non-zero results have 0x or 0X prefixes, respectively. For e, E, f, g, and G conversions, the result usually contains cardinality characters, even if there are no numbers after the cardinality characters. For g and G conversions, the ending zero is not removed as usual.

0 for d, I, o, u, x, e, E, f, g, and G conversions, leading zeros (following symbols or bases) are used to fill the field width and will not be filled with spaces. If the 0 (zero) and-(minus sign) options are displayed, the 0 (zero) option is ignored. For d, I, o, u, x, and X conversions, the 0 (zero) option is ignored if you specify precision.

Note:

Other transformations, whose behavior is not defined.

3. Optional decimal numeric string that specifies the width of the minimum value field. If the conversion value character is less than the field width, the field is populated with the specified field width from left to right. If the left adjustment option is specified, the field is populated on the right. If the conversion result is wider than the field width, the field is expanded to include the converted result. There will be no truncation. However, a small precision may result in truncation on the right.

4. Optional precision. The precision is one. (dot) is followed by a decimal number string. If no precision is given, it is treated as 0 (zero). Precision assignment:

* the minimum number of digits converted by d, o, I, u, x or X.

The minimum number of digits after the cardinality characters converted by * e and f shows the number of digits.

* g the maximum number of significant digits converted.

The maximum number of print bytes of a string in * s conversion.

5. A character that indicates the type of conversion to apply, for example:

% do not convert. Print a% (percent sign).

D, I take an integer value and convert it to a signed decimal symbolic representation. Precision specifies the minimum number of digits displayed. If the value can be expressed in fewer digits after conversion, a leading zero extension is used. The default precision is 1. The result of zero conversion with zero precision is an empty string. Use zeros as leading characters to specify the field width, causing the field width value to be populated with leading zeros.

O accept an integer value and convert it to a signed octal symbolic representation. Precision specifies the minimum number of digits displayed. If the value can be expressed in fewer digits after conversion, a leading zero extension is used. The default precision is 1. The result of zero conversion with zero precision is an empty string. Use zeros as leading characters to specify the field width, causing the field width value to be populated with leading zeros. The field width is not represented by octal values.

U accepts an integer value and converts it to an unsigned decimal symbolic representation. Precision specifies the minimum number of digits displayed. If the value can be expressed in fewer digits after conversion, a leading zero extension is used. The default precision is 1. The result of zero conversion with zero precision is an empty string. Use zeros as leading characters to specify the field width, causing the field width value to be populated with leading zeros.

X, X accepts an integer value and converts it to a hexadecimal symbolic representation. The letter abcdef is used for x conversion and the letter ABCDEF is used for X conversion. Precision specifies the minimum number of digits displayed. If the value can be expressed in fewer digits after conversion, a leading zero extension is used. The default precision is 1. The result of zero conversion with zero precision is an empty string. Use zeros as leading characters to specify the field width, causing the field width value to be populated with leading zeros.

F accepts a floating-point or double-precision value and converts it to a decimal notation in the format [-] ddd.ddd. The number of digits after the cardinality character (shown here as a decimal point) is equal to the specified precision. The LC_NUMERIC locale catalog determines the cardinality characters used in this format. If you do not specify precision, six digits are output. If the precision is 0 (zero), the cardinality characters are not displayed.

E, E accepts a floating-point or double-precision value and converts it to an exponential representation of [-] d.dde {+ | -} dd. There is a number before the cardinality character (shown here as a decimal point), and the number of digits after the cardinality character is equal to the specified precision. The LC_NUMERIC locale catalog determines the cardinality characters used in this format. If you do not specify precision, six digits are output. If the precision is 0 (zero), the cardinality characters are not displayed. The E conversion character generates a number with an E instead of an e before the index. An index usually contains at least two numbers. However, if the index value to be printed is greater than two digits, additional exponential numbers need to be printed if necessary.

G, G accepts floating-point and double-precision values and converts them to the style of f or e converted characters (or E in the case of G conversion), specifying the number of significant digits with precision. The trailing zero is removed from the result. Cardinal characters are displayed only when they are followed by numbers. The style used depends on the value of the conversion. Style g is used only when the converted exponential result is less than-4, or greater than or equal to precision.

C accepts the value as a string and prints the first character in the string.

S accepts the value as a string and prints the characters in the string until the end of the string or the number of characters indicated by the precision. If no precision is specified, all characters are printed until the first empty character appears.

B accepts the value as a string and may contain a backslash escape sequence. Prints bytes from the converted string until the string ends or reaches the number of bytes indicated by the precision specification. If no precision is specified, all bytes are printed until the first empty character appears.

The following backslash escape sequences are supported:

* the previously listed backslash escape sequence is described under the Format parameter. These escape sequences are converted to the individual characters they represent.

The *\ c (backslash c) sequence, which does not display and causes the printf command to ignore all remaining strings, all remaining string arguments, and all additional characters contained in the string argument in the Format parameter.

Exit statu

The command returns the following export values:

0 completed successfully.

An error occurred in > 0.

Exampl

1. Enter the following command:

Printf ("% 5d%4d\ n", 1213Jing 43)

The following output is produced:

_ 1213_ _ 43

Prints all given strings using the Format parameter three times. 0 (zero) is provided by the printf command to meet the final% 4d conversion specification.

two。 Enter the following command

Printf ("% c% c\ n", 78% 79)

The following output is produced:

N_O

This is how printf formats the output function in C language. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report