CTCH 233 mini scanf

advertisement
This presentation includes custom animations.
To view the animations, you must view the presentation in Slide Show mode
and activeX controls must be allowed.
If you have opened this lesson in PowerPoint, use the PowerPoint menus to
view it in slide show mode.
If you have opened this lesson in a browser and see a bar similar to that below,
click on the Slide Show icon
A notice similar to the one below may appear warning that ActiveX or
other scripts are disabled. Enable the controls for this website in order
to see the animations.
scanf
This lesson describes the syntax and use of the scanf library functions:
scanf()
fscanf()
sscanf()
Vocabulary:
ASCII
conversion specifier
file name extension
FILE pointer
flag
fscanf()
placeholder
precision
Click
Tip
scanf()
result
sscanf()
stdin
text file
unicode
To use scanf, fscanf, or sscanf, you must #include <stdio.h>
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
The scanf functions are general-purpose input routines that read from a
stream and store the information in the variables pointed to in the argument list
scanf reads input from stdin.
Click
Tip
stdin is a predefined macro
that represents the standard
output and is typically the
keyboard although this can be
changed by the end user.
fscanf reads input from a text file.
Click
Tip
A text file is one in which
every byte is a character from
the ASCII, Unicode, or other
character set. Some text files
have a filename extension
other than .txt
sscanf reads input from a string variable.
/* VARIABLE DECLARATIONS */
char InputString[81];
3
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
syntax diagrams for the scanf cousins from Appendix B of the text
int scanf (const char *format [, address, …]);
int fscanf (FILE *stream, const char *format [, address, …]);
int sscanf (char *buffer, const char *format [, address, …]);
All 3 return the number of values read in successfully.
All 3 include a control string that specifies what to look for.
All 3 include a set of addresses in which to store the input.
Click
Tip
Although the square brackets indicate that the arguments at the end
are optional, that is a bit misleading. They are required if there are
any placeholders in the output string. There must be one argument
(value) provided for each placeholder.
4
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
The syntax of the cousins varies only in the required arguments.
int scanf (const char *format [, address, …]);
int fscanf (FILE *stream, const char *format [, address, …]);
int sscanf (char *buffer, const char *format [, address, …]);
scanf has 1 required argument. That required argument is the control
string.
fscanf has 2 required arguments. The first argument in fscanf identifies
the file pointer for the destination file and the 2nd argument is the
control string.
sscanf has 2 required arguments. The first argument in sscanf identifies
the destination string variable and the 2nd argument is the control string.
Click
Tip
stdin is treated like a file pointer in C so the following 2 constructions
produce the same result:
scanf("%d", &ID);
fscanf(stdin, "%d", &ID);
5
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.
Keyboard Buffer
1 6 . 5 \t
\.t
1
6
5
6
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.
Keyboard Buffer
1 6 . 5 \t
Notice that hitting the tab key places
the value '\t' in one byte of the
keyboard buffer.
7
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.
Keyboard Buffer
1 6 . 5 \t 2 3 \n
\2
3n
8
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Keyboard Buffer
1 6 . 5 \t 2 3 \n
Notice that hitting the enter/return key, places
the value '\n' in one byte of the keyboard
buffer.
9
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.
Keyboard Buffer
1 6 . 5 \t 2 3 \n 7 5 . 2
7.
5
2
10
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.
Keyboard Buffer
1 6 . 5 \t 2 3 \n 7 5 . 2
Notice that hitting the spacebar, places the
value ' ' in one byte of the keyboard buffer. A
space is a value. It is not the same as NULL.
11
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.
Keyboard Buffer
1 6 . 5 \t 2 3 \n 7 5 . 2
1 8 \n
\n
8
1
12
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
Keyboard Buffer
1 6 . 5 \t 2 3 \n 7 5 . 2
1 8 \n
Now we understand how values get into the keyboard buffer.
The next question is how do they get out of the keyboard buffer and into a variable?
Click to see some C functions that
take values from the keyboard buffer
and store them in a variable.
Click
Tip
scanf
getc
getchar
getch
getche
Actually, the operating system uses API calls as intermediaries
between the application calls and the keyboard buffer but
understanding APIs is beyond the scope of this class and is not
necessary for understanding the C I/O functions. 
13
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
This miniLesson is about scanf so that is what we will look at now.
There are 2 types of arguments in scanf:
int scanf (const char *format [, address, …]);
the control string
The control string is made up
of 3 types of items:
• format specifiers
• white spaces
• non-white space characters
memory addresses
(ie: variables)
Following the control string, the
arguments must be addresses of
variables.
• & precedes the names of
scalar variables
• No & precedes the names
of arrays nor do [ ]’s follow
the name.
scanf(“%d %s”, &age, name);
adapted fromSchildt. C/C++ Programmer’s Reference
14
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
the control string…
int scanf( control string, arg1, …, argn);
Format specifiers
Input format specifiers begin with a sign and tell scanf() what type of data is to be
read next. The format string is read left to right and the format specifiers are
matched, in order, with the arguments that comprise the argument list.
specifier
Reads
%c
a single char
%d
any integer (decimal, octal, or hex)
%i
A decimal integer
%f
a floating-point number
%s
a string
%[ ]
scanset
%%
a percent sign
adapted fromSchildt. C/C++ Programmer’s Reference
15
For each placeholder, there must be a value in the argument list.
The data type of the value must match or be cast as the value indicated by the
format specifier.
The order in which the placeholders appear in the output string must exactly
match the order of the values in the argument list.
printf(“The answer is %d”, 3);
1 placeholder 1 value
printf(“She is %d years old.”, age);
1 placeholder 1 value
printf(“%d + %d = %d”, numA, numB, numA + numB); 3 placeholder 3 values
16
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
int scanf( control string, arg1, …, argn);
Whitespace characters
A whitespace character is either
•a space
•a tab character
•a newline
A whitespace character in the format string causes scanf to skip over zero or
more whitespace characters in the input stream. In essence, one whitespace
character in the control string will cause scanf() to read, but not store, any number
of whitespace characters up to the first nonwhitespace character.
scanf(“\n%d”, &iAge);
Note, including a whitespace at the beginning of the control string will also serve
to flush any newlines at the beginning of the buffer. This is very useful.
adapted fromSchildt. C/C++ Programmer’s Reference
17
int scanf( control string, arg1, …, argn);
Nonwhitespace characters
A nonwhitespace character in the format string causes scanf to read and discard
a matching character. If the specified character is not found, scanf will terminate.
scanf(“%d,%d”, &iAge, &iSize);
The above will work with an input stream of 10,20
It will fail with 10 20
scanf(“%d %d”, &iAge, &iSize);
The reverse is true with the scanf above.
It will FAIL on 10,20
It will succeed with 10 20
adapted fromSchildt. C/C++ Programmer’s Reference
18
int scanf( control string, arg1, …, argn);
scanset
A scanset defines a set of characters that will be read by scanf and assigned to
the corresponding character array. A scanset is defined by putting the characters
you want to scan for inside square braces [ ]/ the beginning square brace must
be prefixed by a percent sign. For example, %[ABC] tells scanf to read only the
chars A, B, and C
When a scanset is used, scanf continues to read characters and put them into the
corresponding character array until a character that is not in the scanset is
encountered. The corresponding variable must be a pointer to a character array.
Upon return from scanf, the array will contain a null-terminated string comprised
of the characters read.
inverted scanset
Including a carat as the first character in a scanset instructs scanf to accept any
^
character NOT included in the scan set.
%[^XYZ]
adapted fromSchildt. C/C++ Programmer’s Reference
19
the control string…
3 characters are considered "whitespace":
• \t
(Tab)
• \n (newline created by hitting the Enter/Return key)
•
(a blank space)
Keyboard Buffer
1 6 . 5 \t 2 3 \n 7 5 . 2
1 8 \n
There are 4 whitespaces in the keyboard buffer above.
20
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
scanf treats a set of contiguous whitespace characters as 1 single whitespace.
Keyboard Buffer
1 6 . 5 \t \t \t 2 3
1 whitespace
\n 7 5
.
2
1 whitespace
There are 2 whitespaces in the keyboard buffer above.
21
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
scanf "scans" the keyboard buffer to find the items specified in the control string.
scanf("%f" , &AvgAmount);
This statement says to read a float from the keyboard buffer and store it in AvgAmount.
22
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
scanf("%f" , &AvgAmount);
Click for
Step 1:
scanf starts at the beginning of the keyboard buffer and reads until it
encounters a whitespace or a character that cannot be part of the float.
Keyboard Buffer
1 6 . 5 \t 2 3 \n 7 5 . 2
Click for
Step 2:
The value is stored in the variable, AvgAmount
AvgAmount
Click for
Step 3:
1 8 \n
16.5
The stored value is removed from the keyboard buffer and everything
moves up. (Notice that the whitespace is still in the buffer.)
Keyboard Buffer
\t 2 3 \n 7 5
. 2
1 8 \n
23
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
The keyboard buffer waits in this state for the next call that accesses the keyboard.
Keyboard Buffer
\t 2 3
\n
7 5 . 2
1 8
\n
24
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
scanf(“\n%d %f" , &ItemCount, &ItemAmount);
This statement says to read a whitespace from the keyboard buffer
followed by an integer
followed by a whitespace
followed by a float
and to store the integer in ItemCount and the float in ItemAmount.
25
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
scanf(“\n%d %f" , &ItemCount, &ItemAmount);
Starting
memory
Keyboard Buffer
Click for
after Step 1:
Keyboard Buffer
Click for
after Step 2:
Keyboard Buffer
Click for
after Step 3:
Keyboard Buffer
Click for
after Step 4:
Keyboard Buffer
\t 2 3
2 3
\n
\n
\n
7 5 . 2
7 5 . 2
\n
1 8
\n
ItemAmount
ItemCount
7 5 . 2
7 5 . 2
1 8
ItemCount
1 8
\n
ItemAmount
ItemCount
1 8
1 8
\n
ItemAmount
ItemCount
\n
23
23
ItemAmount
ItemCount
ItemAmount
23
75.2
Use fflush(stdin); to empty the keyboard buffer.
Keyboard Buffer
1 6 . 5 \t 2 3 \n 7 5 . 2
1 8 \n
27
In this class, unless you have a specific reason to do otherwise,
ALWAYS begin your scanf statements as follows:
fflush(stdin);
scanf(“\n
Why?
The fflush statement will remove any typeahead characters from the
keyboard buffer ensuring that the keyboard buffer contents were
entered after the user read your prompt.
The \n will cause the scanf statement to ignore any whitespace that
appears before the content you want to retrieve from the input
stream.
It bears repeating…
ALWAYS begin your scanf statements as follows:
Click Tip
fflush(stdin);
scanf(“\n
…if you don’t, don’t say you weren’t warned!!!
28
Download