Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to use printf output string in c language?
How to use printf output string in c language?
How to use the common format of printf output string printf function in C language

Printf (format control string, output table column);

Take figure 1 as an example.

Figure 1.

Functional parameters include two parts:

The 1 format control string is a string enclosed in double quotation marks, also known as the conversion control string, which specifies the type and format of the output data item.

It includes two kinds of information:

● Format descriptor: it consists of% and format characters, such as% d,% f, etc. Format descriptions always start with% characters and end with format characters. Its function is to convert the output data items into the specified format for output. Each data item in the output table column corresponds to a format descriptor.

● Ordinary characters: that is, characters that need to be output as they are, such as commas and line breaks.

The output list is some data items that need to be output, which can be expressions.

For example, a = 3 and b = 4, then printf ("a =% db =% d ",a, b); , output a = 3b = 4. Two? "%d" is a format description, which means to output two integers, corresponding to variables A and B respectively. "A =" and "B =" are ordinary characters and are output as they are.

Because printf is a function, the format control string and output table columns are actually parameters of the function. The general form of printf function can be expressed as:

Printf (parameter 1, parameter 2, parameter 3, …, parameter n)

The function of printf is to output parameter 2 to parameter n according to the given format of parameter 1.

3 format characters? (constitutes a format descriptor)

For different types of data items, format descriptors composed of different format characters should be used. The following are commonly used format characters: (List the common usage of various format characters according to different data types)

D format characters are used to output decimal integers. There are the following usages:

●? % d, output according to the actual length of data.

●? % md, m specifies the width of the output field? (integer). If the number of digits of the data is less than m, the left end is filled with spaces (right-aligned); If it is greater than m, it is output according to the actual number of digits.

●? %-MD, m specifies the width of the output field? (integer). If the number of digits of the data is less than m, the right end is filled with spaces? (left alignment); If it is greater than m, it is output according to the actual number of digits.

●? % ld, output long integer data, or specify a width of% %mld.

O format characters output integers in octal form. Note: the value of each bit in the storage unit is output in octal form, and the output data is unsigned, that is, the sign bit is also output as a part of octal.

For example:

inta =- 1;

printf("%d,%o,%x ",a,a,a);

The original code is-1:1000,000,0001.

The complement of-1 in memory is expressed as:

1 1 1 1, 1 1 1, 1 1 1, 1 1 1 1 1 1 1 1 1 1 = 1

Output:-1, 177777, ffff.

S-format characters are used to output strings. There are the following usages:

●? % s, output string.

●? % ms, the output string occupies m columns; If the string length is greater than m, all strings are output; If the length of the string is less than m, fill the space on the left? (Right alignment).

●? %-ms, the output string accounts for m columns; If the string length is greater than m, all strings are output; If the length of the string is less than m, fill in the space on the right? (Left alignment).

●? % %m.ns, the output occupies m columns, but only the left n characters of the string are taken, and the left is filled with spaces? (Right alignment).

●? %-m.ns, the output occupies m columns, but the left end of the string only takes n characters, and the right end is filled with spaces? (Left alignment).

F format characters are used to output real numbers? (including single precision and double precision, single precision and double precision format symbols are the same), and output in decimal form. There are the following usages:

●? % f, no width is specified, so all integer parts are output, and 6 decimal places are output. Note that not all digits are significant, and the significant digits of single-precision real numbers are usually 7 digits? (double precision 16 bits).

●? % %m.nf, the specified data occupies m columns, including n decimal places. If the length of the value is less than m, the left end is filled with spaces (right-aligned).

●? %-m.nf, the specified data occupies m columns, including n decimal places. If the length of the value is less than m, the right end is filled with spaces (left-aligned).

E format characters output real numbers in exponential form. Can be expressed in the following form:

●? % e, the width and decimal places occupied by the output data are not specified, but are automatically specified by the system. For example, there are six decimal places, the exponent occupies five places, -E occupies 1 place, the exponent symbol occupies 1 place, and the exponent occupies three places. The numerical value is output in the form of normalized exponent? (There must be only 1 nonzero digits before the decimal point).