printf的用法(The use of the printf)
printf的用法(The use of the printf)
1. Printf () function
The printf () function is a formatted output function that is typically output to standard output devices in a specified format
Information. This is often used when writing programs. The call format of the printf () function is:
Printf (< formatted string >, < parameter table >);
The formatted string consists of two parts: one is the normal character, the other is the original character
Another part is formatting the required characters, beginning with '%', followed by one or more specified characters,
Used to determine the format of the output content.
A parameter table is a series of parameters that need to be output, and the number must be the output specified by the formatted string
A number of parameters, among the parameters "," separate and sequential correspondence, otherwise there will be unexpected
Errors that cannot be reached.
1. formatting specifications
The formatting specifications provided by Turbo C2.0 are as follows:
Thought of thought - thought of thought
Symbolic action
- - - - - - - - - - - - - - - -
%d decimal signed integer
%u decimal unsigned integer
%f floating point numbers
%s string
%c single character
The value of the%p pointer
Floating point numbers in the%e exponent
%x,%X unsigned integer represented by sixteen decimal
%0 unsigned integer represented by octal notation
The%g automatically selects the appropriate representation
Thought of thought - thought of thought
Explain:
(1) you can insert numbers between "%" and "letters" to represent the maximum field width.
For example:%3d means output 3 bit integers, not 3 bit, right justified.
%9.2f represents the floating-point number of the output field width of 9, in which the decimal digit is 2 and the integer bit is 6,
The decimal point is one, not 9, right justified.
%8s stands for a string of 8 characters, not 8 characters, right justified.
If the length or integer number of the string exceeds the indicated field width, it will be output at its actual length.
But for floating point numbers, if the integer bits exceed the indicated integer bit width, they are output by actual integer;
If the decimal portion exceeds the indicated decimal width, the output is output from four to five at the width of the description.
In addition, if you want to add some 0 before the output value, you should add a 0 before the width of the event.
For example:%04d means that when you output a value less than
4 bits, you will fill 0 in front to make the total width
For 4.
If floating-point numbers are used to represent the character or integer output format, the digits after the decimal point represent the maximum width,
The number before the decimal point represents the minimum width.
For example,%6.9s means a string that is no longer than 6 and not greater than 9. If greater than 9, then
After ninth characters, the contents will be deleted.
(2) you can add a lowercase letter L between `% 'and' letter ', which means that the output is long.
For example:%ld means output long integers
%lf means output double floating point numbers
(3) you can control the output left aligned or right aligned, i.e., add a "-" number between "%" and "letter"
Indicates that the output is left justified, otherwise right justified.
For example:%-7d means output 7 bit integers left justified
%-10s stands for output, 10 characters left justified
2. some special characters
Thought of thought - thought of thought
Character action
??????????????????????????
\n换行
清屏并换页\ f
回车\r
没有标签符
“夏侯和表示一个ASCII码用16进表示,
其中HH是1到2个16进制数
??????????????????????????
由本节所学的printf()函数,并结合上一节学习的数据类型,编制下面的程
序,以加深对Turbo C2.0数据类型的了解。
例1
#包括< stdio. h >
#包括<字符串。”
国际main()
{
字符c,s [ 20 ],*;
int = 1234,* i;
浮点f,3.141592653589;
双x,0.1234567898765432;
“你好”;
strcpy(s,“你好,同志”);
* i,12;
C =“X41的;
printf(“= %d\n”,a);/*结果输出十进制整数= 1234 * /
printf(“= % 6d \n”,a);/*结果输出6位十进制数= 1234 * /
printf(“= % 06d \n”,a);/*结果输出6位十进制数= 001234 * /
printf(“= %的二维N”,a);/*一超过2位,按实际值输出= 1234 * /
printf(“*我=第%4d \n”,*我);/*输出4位十进制整数*我= 12 * /
printf(“*我= %四维\n”,*我);/*输出左对齐4位十进制整数*我= 12 * /
printf(“我= % P n”,我);/*输出地址我= 06E4 */
printf(“F = F,F为\n”);/*输出浮点数f = 3.141593 * /
printf(“F = 6.4f \n”,F);/*输出6位其中小数点后4位的浮点数
f,3.1416 *
printf(“X = %如果\n”,x);/*输出长浮点数x = 0.123457 * /
printf(“X = % 18.16lf \n”,x);/*输出18位其中小数点后16位的长浮点
数x = 0.1234567898765432 * /
printf(“C = C,C为\n”);/*输出字符C = A /
printf(“C = % X N,C);/*输出字符的ASCII码值C = 41 * /
printf(“[ ] = %s \n”,S);/*输出数组字符串的[ ] =你好,同志/
printf(“[ ] = % 6.9s \n”,S);/*输出最多9个字符的字符串的[ ] =你好,
公司/
printf(“S = % P N,S);/*输出数组字符串首字符地址= ffbe */
printf(“* P = %s \n”,p);/*输出指针字符串P =你怎么做/
printf(“P = p n,p);/*输出指针的值P = 0194 * /
getch();
返还0;
}
上面结果中的地址值在不同计算机上可能不同。