|
|
1.1 root 1:
2:
3: printf() STDIO printf()
4:
5:
6:
7:
8: Print formatted text
9:
10: iinntt pprriinnttff(_f_o_r_m_a_t [,_a_r_g_1, .... _a_r_g_N])
11: cchhaarr *_f_o_r_m_a_t; [ddaattaa ttyyppee] _a_r_g_1, ... _a_r_g_N;
12:
13: printf prints formatted text. It uses the format string to
14: specify an output format for each arg, which it then writes on
15: the standard output.
16:
17: printf reads characters from format one at a time; any character
18: other than a percent sign `%' or a string that is introduced with
19: a percent sign is copied directly to the output. A `%' tells
20: printf that what follows specifies how the corresponding arg is
21: to be formatted; the characters that follow `%' can set the out-
22: put width and the type of conversion desired. The following
23: modifiers, in this order, may precede the conversion type:
24:
25: 11. A minus sign `-' left-justifies the output field, instead of
26: the default right justify.
27:
28: 22. A string of digits gives the width of the output field. Norm-
29: ally, printf pads the fiel padded with spaces to the field
30: width; it is padded on the left unless left justification is
31: specified with a `-'. If the field width begins with `0', the
32: field is padded with `0' characters instead of spaces; the `0'
33: does not cause the field width to be taken as an octal number.
34: If the width specification is an asterisk `*', the routine
35: uses the next arg as an integer that gives the width of the
36: field.
37:
38: 33. A period `.' followed by one or more digits gives the preci-
39: sion. For floating point (ee, ff, and gg) conversions, the
40: precision sets the number of digits printed after the decimal
41: point. For string (ss) conversions, the precision sets the
42: maximum number of characters that can be used from the string.
43: If the precision specification is given as an asterisk `*',
44: the routine uses the next arg as an integer that gives the
45: precision.
46:
47: 44. The letter `ll' before any integer conversion (dd, oo, xx, or uu)
48: indicates that the argument is a long rather than an int.
49: Capitalizing the conversion type has the same effect; note,
50: however, that capitalized conversion types are not compatible
51: with all C compiler libraries, or with the ANSI standard.
52: This feature will not be supported in future editions of
53: COHERENT.
54:
55: The following format conversions are recognized:
56:
57: % Print a `%' character. No arguments are processed.
58:
59: cc Print the int argument as a character.
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: printf() STDIO printf()
70:
71:
72:
73: dd Print the int argument as signed decimal numerals.
74:
75: DD Print the long argument as signed decimal numerals.
76:
77: ee Print the float or double argument in exponential form. The
78: format is _d._d_d_d_d_d_dee_s_d_d, where there is always one digit before
79: the decimal point and as many as the precision digits after it
80: (default, six). The exponent sign _s may be either `+' or `-'.
81:
82: ff Print the float or double argument as a string with an
83: optional leading minus sign `-', at least one decimal digit,
84: a decimal point (`.'), and optional decimal digits after the
85: decimal point. The number of digits after the decimal point
86: is the precision (default, six).
87:
88: gg Print the float or double argument as whichever of the formats
89: d, e, or f loses no significant precision and takes the least
90: space.
91:
92: oo Print the int argument in unsigned octal numerals.
93:
94: OO Print the long argument in unsigned octal numerals.
95:
96: rr The next argument points to an array of new arguments that may
97: be used recursively. The first argument of the list is a cchhaarr
98: * that contains a new format string. When the list is ex-
99: hausted, the routine continues from where it left off in the
100: original format string.
101:
102: ss Print the string to which the cchhaarr * argument points.
103: Reaching either the end of the string, indicated by a null
104: character, or the specified precision, will terminate output.
105: If no precision is given, only the end of the string will ter-
106: minate.
107:
108: uu Print the int argument in unsigned decimal numerals.
109:
110: UU Print the long argument in unsigned decimal numerals.
111:
112: xx Print the int argument in unsigned hexadecimal numerals.
113:
114: XX Print the long argument in unsigned hexadecimal numerals.
115:
116: ***** Example *****
117:
118: The following example demonstrates many pprriinnttff statements.
119:
120:
121: main()
122: {
123: extern void demo_r();
124: int precision = 1;
125: int integer = 10;
126: float decimal = 2.75;
127: double bigdec = 27590.21;
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: printf() STDIO printf()
136:
137:
138:
139: char letter = 'K';
140: char buffer[20];
141:
142:
143:
144: strcpy (buffer, "This is a string.\n");
145:
146:
147:
148: printf("This is an int: %d\n", integer);
149: printf("This is a float: %f\n", decimal);
150: printf("Another float: %3.*f\n", precision, decimal);
151: printf("This is a double: %lf\n", bigdec);
152: printf("This is a char: %c\n", letter);
153: printf("%s", buffer);
154: printf("%s\n", "This is also a string.");
155:
156:
157:
158: demo_r("Print everything: %d %f %lf %c",
159: integer, decimal, bigdec, letter);
160: exit(0);
161: }
162:
163:
164:
165: void demo_r(string)
166: char *string;
167: {
168: printf("%r\n", (char **)&string);
169: }
170:
171:
172: ***** See Also *****
173:
174: fprintf(), putc(), puts(), scanf(), sprintf(), STDIO
175:
176: ***** Notes *****
177:
178: Because C does not perform type checking, it is essential that
179: each argument match its counterpart in the format string.
180:
181: The use of upper-case format characters to specify long arguments
182: is not standard, and will be phased out to conform with the ANSI
183: standard. You should use the `l' modifier to indicate a lloonngg.
184:
185: At present, pprriinnttff does not return a meaningful value.
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196: COHERENT Lexicon Page 3
197:
198:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.