C Programming Tutorial (09) - Detailed Usage of Data Output Function (printf)

C Programming Tutorial (09) - Detailed Usage of Data Output Function (printf)

insert image description here

This column mainly introduces the basic syntax of C language, which is used as the courseware and reference materials of the "Programming Language" course for the teaching of the "Programming Language" course, for entry-level users to read.

1. Assignment statement in C language

Assignment statement, same as assignment expression, is one of the most used statements in a program. The format of the assignment statement is as follows:

赋值表达式;

illustrate:

(1) When a variable has no data, through assignment, the value of the expression at the right end of the assignment number is stored in the storage unit represented by the variable.

(2) When a variable has obtained data, through assignment, store the value of the expression at the right end of the assignment number into the storage unit represented by the variable, and overwrite the original value.

(3) When reading the value of the variable, the value of the variable does not disappear.

(4) Since the expression on the right side of the assignment operator (=) can be an assignment expression. Therefore, a statement of the form a=b=b=d=5; is correct. According to the right associativity of the assignment operator, this statement is actually equivalent to:

d=5;
c=5;
b=5;
a=5;

(5) Pay attention to the difference between assigning an initial value to a variable and an assignment statement in the variable description. Assigning an initial value to a variable is a part of the variable description. The variable after the initial value assignment must be separated from other variables of the same type after it with a comma, and the assignment statement must end with a semicolon. For example:

int i=1,j=2;
int a,b;
a=1;
b=2;

(6) Strictly distinguish between assignment expressions and assignment statements. An assignment expression is an expression that can appear anywhere an expression is allowed, whereas an assignment statement cannot.

2. Data output - printf function

In order to realize input and output functions, C language provides a set of input and output functions in its library functions: putchar (output character), getchar (input character), printf (formatted output), scanf (formatted input). Among them, the scanf and printf functions are for standard input and output devices (keyboard and display) to format input and output functions. Since they are defined in the file stdio.h, when using them, this file should be included into the program file using the compile preprocessing command #include<stdio.h>.

1. The general form of the printf function

The printf function is called a formatted output function, and the format of the printf function is:

printf("格式控制字符串",输出项列表);

illustrate:

(1) "Format control string" is a string enclosed in double quotation marks, including the following two types of information:

① Format specifier: The function is to convert the data to be output into the specified format for output. It consists of "%" and format characters, such as: %d, %f, etc.

② Ordinary characters: the characters that are output as they are.

For example:

printf("a=%d,b=%d\n",a,b);

Among them: %d is a format specifier, and the rest are ordinary characters (including a=, comma, b=, \n). If a=10, b=20, the output of the output statement is:

a=10, b=20

(2) The output list is the data that needs to be output. The items in the output list should be separated by commas. Each output item can be a legal constant, variable, expression or function. The number of format specifiers must be the same as the number of output items, in a one-to-one correspondence and with matching types. For example:

int a=20;
float b=13.5;
printf("a=%d,b=%f\n",a,b);

Since printf is a function, "format control character" and "output list" are actually parameters of the function. The general form of the printf function can also be expressed as:

printf(参数1, 参数2, ... ,参数n);

The essence of the printf function is to output parameters 2~parameter n in the format given by parameter 1.

2. Format characters

Different format characters are used for different types of data, and the following format characters are commonly used:

(1) d format character

Used to output decimal integers.

① %d, output according to the actual length of the integer data.

② %md, m is the width of the specified output data. If the number of digits of the data is less than m, the left end will be filled with spaces, if it is greater than m, it will be output according to the actual number of digits.

For example:

int a=134, b=12345;
printf("%5d,%4d",a,b); //输出结果为:  134(左侧补两个空格), 12345

③ %ld, output long integer data

long a=123456;
printf("%ld",a);
//如果按%d输出,会发生错误
//长整型也可以指定宽度,如:printf("%7ld",a);
//一个int型数据可以用%d或%ld格式输出

(2) o format character

Output integers in octal form. Since the value (0 or 1) of each bit in the memory unit is output in octal form, the output value is unsigned, that is, the sign bit is also output together as part of the octal number. For example:

int a=-1;
printf("%o",a); //输出结果为:177777

For long integer (long type), you can use the %lo format to output, and you can also specify the output width, for example:

int a=-1;
printf("%8lo",a); //输出结果为:  177777

(3) x format character

Output integers in hexadecimal form. Negative hexadecimal numbers also do not appear.

For example:

int a=-1;
printf("%d,%x",a,a); //输出结果为:-1,ffff

You can use %lx to output a long integer, and you can also specify the width of the output data, such as %12x, etc.

(4) u format character

Output unsigned type data in decimal.

A signed integer (int) can also be output in %u format; conversely, an unsigned type data can also be output in %d format. Unsigned data can also be output with %o or %x.

(5) c format character

Used to output a character. For example:

char ch='A';
printf("%c\n",ch);  //输出结果:A

An integer, as long as its value is between 0 and 255, can also be output in character form. Before outputting, convert the integer into ASCII characters, otherwise, a character data can also be output in the form of integers. For example:

char ch='A';
int x=97;
printf("%c",ch); //输出:A
printf("%d",ch); //输出:65
printf("%c",x); //输出:a
printf("%d",x); //输出:97

(6) s format character

Used to output a string.

① %s, output the string according to the actual format.

For example:

printf("%s","China");  //输出结果:China

② %ms, the output string occupies m columns. If the length of the string itself is greater than m, it will break through the limit of m and output all the strings. If the character string is smaller than m, spaces are filled on the left. For example:

printf("%10s","China");  //输出结果:     China

③%-ms, the output string occupies m columns. If the length of the string itself is greater than m, it will break through the limit of m and output all the strings. If the string is smaller than m, the string is left-aligned and spaces are filled on the right. For example:

printf("%10s%d","China",20);  //输出结果:China     20

④ %m.ns, the output occupies m columns, but only n characters from the left end of the string are taken, and the n characters are output at the right end of m columns, with blanks on the left. For example:

printf("%5,2s","China");  //输出结果:   Ch

⑤ %-m.ns, the output occupies m columns, but only n characters from the left end of the string are taken, and the n characters are output at the left end of m columns, and spaces are filled on the right. For example:

printf("%-5,2s,%d","China",20);  //输出结果:Ch   20

(7) f format character

Output real numbers as decimals. (including single precision, double precision)

① %f, the system automatically specifies the output width, so that all integer parts are output and 6 decimal places are output.

② %m.nf, the specified output data occupies m columns in total, and there are n decimal places. If the length of the data is less than m, blanks are filled at the left end.

③ %-m.nf, the specified output data occupies m columns in total, and there are n decimal places. If the length of the data is less than m, the data is left-aligned and spaces are filled at the right end.

For example:

#include<stdio.h>
int main() {
    
           
	float f=123.456;
	printf("%f,%10f,%10.2f,%-10.2f,%.2f",f,f,f,f,f);
	return 0;
} 

The result of running the above program is as follows:

insert image description here

(8) e format character

Output real numbers in exponential form.

① %e, do not specify the width of the output data and the number of decimal places in the number part, the system will automatically give 6 decimal places, and the exponent part occupies 5 digits (such as e+013), where e occupies one digit and the exponent symbol occupies one digits, and the exponent occupies 3 digits. For example:

printf("%e",123.456); //输出:1.234560e+002

② %m.ne and %-m.ne, the specified output data occupies m columns in total, and there are n decimal places.

(9) g format character

It is used to output real numbers. According to the size of the value, the f format or e format is automatically selected (the one with the smaller width is selected for output), and meaningless zeros are not output.

3. Summary of format character usage of printf function

Format characters for the printf function

format character illustrate
d,i Output integers in signed decimal form (positive numbers are unsigned)
o Output integers in octal unsigned form (do not output leading 0s)
x,X Output integers in hexadecimal unsigned form (do not output the leading character 0x), use lowercase x to output hexadecimal numbers a~f in lowercase, and use uppercase X to output in uppercase.
u Output integers in unsigned decimal form.
c Output as characters, one character at a time.
s output string.
f Outputs single and double precision types as decimals, implicitly outputting 6 decimal places.
and and Outputs a real number with exponent e or E.
g,G Choose the format with shorter width in the %f or %e format, and do not enter meaningless 0. When G is used, if the output is in exponential form, the exponent will be expressed in capital letters.

Note: If the character % needs to be output, it should be represented by two consecutive % in the "format control string". For example:

printf("%f%%",0.5); //输出结果:0.50000%

In the format specification, the following additional symbols can be inserted between % and the format characters in the above table.

(1) Logo characters

Flag characters include -, +, and #, and the specific meanings are shown in the following table:

character significance
- The output results are left-aligned, and blanks are filled on the right; by default, the output results are right-aligned, and blanks are filled on the left
+ When the output value is positive, it is marked with a "+" sign, and when it is negative, it is marked with a "-" sign
# Prefix 0 for octal output; prefix 0x for hexadecimal output

For example:

printf("%6d,%d\n",111,2); //输出结果:   111,2
printf("%-6d,%d\n",111); //输出结果:111   ,2
printf("%+d,%+d\n",111,-111); //输出结果: +111,-111
printf("%#o,%#x\n",10,16); //输出结果:012,0x10

(2) Width indicator

It is used to set the minimum width of the output data item, usually a decimal integer is used to indicate the number of output digits. If the actual number of digits required for the output data item is more than the specified width, it will be output according to the actual number of digits, and if the actual number of digits is less than the specified width, it will be filled with spaces. For example:

printf("%d,%d\n",888,12);  //输出结果:888(按实际需要宽度输出),12
printf("%6d,%d\n",888,12);  //输出结果:   888(左边补3个空格),12
printf("%-6d,%d\n",888,12);  //输出结果:888   ,12(右边补3个空格)
printf("%f,%d\n",888.88,12);  //输出结果:888.880000(按实际需要宽度输出),12
printf("%12f,%d\n",888,12);  //输出结果:  888.880000(左边补2个空格),12
printf("%g,%d\n",888,12);  //输出结果:888.88(%f格式比%e格式输出宽度小),12
printf("%8g,%d\n",888,12);  //输出结果:  888.88(左边补2个空格),12

(3) Accuracy indicator

Beginning with ".", specify its precision with a decimal integer. For float and double types of floating-point numbers, you can use the form of mn to specify its precision while specifying its width. Among them, m is used to specify the total width of the output data, and n is called the precision. For example:

printf("%.5d\n",888);  //输出结果:00888(数字前补0)
printf("%.0d\n",888);  //输出结果:888
printf("%d,%8.3f\n", 11, 888.88);  //输出结果:11, 888.880(左边补一个空格)
printf("%d,%8.1f\n", 11, 888.88);  //输出结果:11,   888.9(左边补3个空格)
printf("%d,%8.0f\n", 11, 888.88);  //输出结果:11,     888(左边补5个空格)
printf("%.5s","abcdefg");  //输出结果:abcde(截去超过的部分)
printf("%5s","abcdefg");  //输出结果:abcdefg(宽度不够,按实际宽度输出)

(4) Length modifier

Commonly used length modifiers are h and l. h indicates that the output item is output as a short integer, and l indicates that the output item is output as a long integer.

Example of data output:

#include<stdio.h>
int main() {
    
           
	int n1=123;
	long n2=1234567;
	float real=123.4567;
	printf("%d,%6d,%-6d,%2d\n",n1,n1,n1,n1);
	printf("%ld,%8ld,%4ld\n",n2,n2,n2);
	printf("%f,%10f,%10.2f,%-10.2f",real,real,real,real);
	printf("%s,%10.5s,%-10.5s","student","student","student");
	return 0;
} 

The result of running the above program is as follows:

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/128610865