|
|
1.1 root 1:
2:
3: scanf() STDIO scanf()
4:
5:
6:
7:
8: Accept and format input
9:
10: #include <stdio.h>
11: iinntt ssccaannff(_f_o_r_m_a_t, _a_r_g_1, ... _a_r_g_N)
12: cchhaarr *_f_o_r_m_a_t; [ddaattaa ttyyppee] *_a_r_g_1, ... *_a_r_g_N;
13:
14: scanf reads the standard input, and uses the string format to
15: specify a format for each arg1 through argN, each of which must
16: be a pointer.
17:
18: scanf reads one character at a time from format; white space
19: characters are ignored. The percent sign character `%' marks the
20: beginning of a conversion specification. `%' may be followed by
21: characters that indicate the width of the input field and the
22: type of conversion to be done.
23:
24: scanf reads the standard input until the return key is pressed.
25: Inappropriate characters are thrown away; e.g., it will not try
26: to write an alphabetic character into an iinntt.
27:
28: The following modifiers can be used within the conversion string:
29:
30: 11. The asterisk `*', which indicates that the next input field
31: should be skipped rather than assigned to the next arg.
32:
33: 22. A string of decimal digits, which specifies a maximum field
34: width.
35:
36: 33. An l, which specifies that the next input item is a long ob-
37: ject rather than an int object. Capitalizing the conversion
38: character has the same effect.
39:
40: The following conversion specifiers are recognized:
41:
42: cc Assign the next input character to the next arg, which should
43: be of type cchhaarr *.
44:
45: dd Assign the decimal integer from the next input field to the
46: next arg, which should be of type iinntt *.
47:
48: DD Assign the decimal integer from the next input field to the
49: next arg, which should be of type lloonngg *.
50:
51: ee Assign the floating point number from the next input field to
52: the next arg, which should be of type ffllooaatt *.
53:
54: EE Assign the floating point number from the next input field to
55: the next arg, which should be of type ddoouubbllee *.
56:
57: ff Same as ee.
58:
59: FF Same as EE.
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: scanf() STDIO scanf()
70:
71:
72:
73: oo Assign the octal integer from the next input field to the next
74: arg, which should be of type iinntt *.
75:
76: OO Assign the octal integer from the next input field to the next
77: arg, which should be of type lloonngg *.
78:
79: ss Assign the string from the next input field to the next arg,
80: which should be of type cchhaarr *. The array to which the char *
81: points should be long enough to accept the string and a ter-
82: minating null character.
83:
84: xx Assign the hexadecimal integer from the next input field to
85: the next arg, which should be of type iinntt *.
86:
87: XX Assign the hexadecimal integer from the next input field to
88: the next arg, which should be of type lloonngg *.
89:
90: It is important to remember that ssccaannff reads up, but not through,
91: the newline character: the newline remains in the standard input
92: device's buffer until you dispose of it somehow. Programmers
93: have been known to forget to empty the buffer before calling
94: ssccaannff a second time, which leads to unexpected results.
95:
96: ***** Example *****
97:
98: The following example uses ssccaannff in a brief dialogue with the
99: user.
100:
101:
102: #include <stdio.h>
103:
104: main()
105: {
106: int left, right;
107:
108:
109:
110: printf("No. of fingers on your left hand: ");
111: /* force message to appear on screen */
112: fflush(stdout);
113: scanf("%d", &left);
114:
115:
116:
117: /* eat newline char */
118: while(getchar() != '\n')
119: ;
120:
121:
122:
123: printf("No. of fingers on your right hand: ");
124: fflush(stdout);
125: scanf("%d", &right);
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: scanf() STDIO scanf()
136:
137:
138:
139:
140:
141: /* again, eat newline */
142: while(getchar() != '\n')
143: ;
144:
145:
146:
147: printf("You've %d left fingers, %d right, & %d total\n",
148: left, right, left+right);
149: }
150:
151:
152: ***** See Also *****
153:
154: fscanf(), sscanf(), STDIO
155:
156: ***** Diagnostics *****
157:
158: scanf returns the number of arguments filled. It returns EOF if
159: no arguments can be filled or if an error occurs.
160:
161: ***** Notes *****
162:
163: Because C does not perform type checking, it is essential that an
164: argument match its specification. For that reason, scanf is best
165: used to process only data that you are certain are in the correct
166: data format. The use of upper-case format characters to specify
167: long arguments is not standard; use the `l' modifier for por-
168: tability.
169:
170: scanf is difficult to use correctly, and its misuse can be as-
171: sociated with intermittent and dangerous bugs. Rather than use
172: scanf to obtain a string from the keyboard: it is recommended
173: that you use gets to obtain the string, and use strtok or sscanf
174: to parse it.
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
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.