This presentation includes custom animations.

28
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.

description

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. - PowerPoint PPT Presentation

Transcript of This presentation includes custom animations.

Page 1: This presentation includes custom animations.

This presentation includes custom animations.

To view the animations, you must view the presentation in Slide Show modeand 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.

Page 2: This presentation includes custom animations.

scanf

Christine S. WolfeOhio University Lancaster2008-Aug-01

This lesson describes the syntax and use of the scanf library functions:scanf()fscanf()sscanf()

Vocabulary:

To use scanf, fscanf, or sscanf, you must #include <stdio.h>ClickTip

ASCIIconversion specifier file name extensionFILE pointerflagfscanf()placeholderprecision

scanf()resultsscanf()stdintext fileunicode

Page 3: This presentation includes custom animations.

3Christine S. WolfeOhio University Lancaster2008-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. stdin is a predefined macro that represents the standard output and is typically the keyboard although this can be changed by the end user.

ClickTip

ClickTip

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.

fscanf reads input from a text file.

/* VARIABLE DECLARATIONS */char InputString[81];

Page 4: This presentation includes custom animations.

4Christine S. WolfeOhio University Lancaster2008-Aug-01

syntax diagrams for the scanf cousins from Appendix B of the text

All 3 include a set of addresses in which to store the input.

All 3 include a control string that specifies what to look for.

All 3 return the number of values read in successfully.

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.

ClickTip

int scanf (const char *format [, address, …]);

int fscanf (FILE *stream, const char *format [, address, …]);

int sscanf (char *buffer, const char *format [, address, …]);

Page 5: This presentation includes custom animations.

5Christine S. WolfeOhio University Lancaster2008-Aug-01

The syntax of the cousins varies only in the required arguments.

sscanf has 2 required arguments. The first argument in sscanf identifies the destination string variable and the 2nd 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.

scanf has 1 required argument. That required argument is the control string.

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);

ClickTip

int scanf (const char *format [, address, …]);

int fscanf (FILE *stream, const char *format [, address, …]);

int sscanf (char *buffer, const char *format [, address, …]);

Page 6: This presentation includes custom animations.

6Christine S. WolfeOhio University Lancaster2008-Aug-01

Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.

Keyboard Buffer

1

1 6 . 5 \t

6.5\t

Page 7: This presentation includes custom animations.

7Christine S. WolfeOhio University Lancaster2008-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.

Page 8: This presentation includes custom animations.

8Christine S. WolfeOhio University Lancaster2008-Aug-01

Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.

Keyboard Buffer

1 6 . 5

23\n

\t 2 3 \n

Page 9: This presentation includes custom animations.

9Christine S. WolfeOhio University Lancaster2008-Aug-01

Keyboard Buffer

1 6 . 5 2\t 3 \n

Notice that hitting the enter/return key, places the value '\n' in one byte of the keyboard buffer.

Page 10: This presentation includes custom animations.

10Christine S. WolfeOhio University Lancaster2008-Aug-01

Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.

Keyboard Buffer

7

7 5 . 2

5.2

1 6 . 5 2\t 3 \n

Page 11: This presentation includes custom animations.

11Christine S. WolfeOhio University Lancaster2008-Aug-01

Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.

Keyboard Buffer

7 5 . 2 1 6 . 5 2\t 3 \n

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.

Page 12: This presentation includes custom animations.

12Christine S. WolfeOhio University Lancaster2008-Aug-01

Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM.

Keyboard Buffer

7 5 . 2 1 8 \n

18\n

1 6 . 5 2\t 3 \n

Page 13: This presentation includes custom animations.

13Christine S. WolfeOhio University Lancaster2008-Aug-01

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?

Keyboard Buffer

7 5 . 2 1 8 \n1 6 . 5 2\t 3 \n

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.

ClickTip

Click to see some C functions that take values from the keyboard buffer and store them in a variable.

scanfgetcgetchargetchgetche

Page 14: This presentation includes custom animations.

14Christine S. WolfeOhio University Lancaster2008-Aug-01

The control string is made up of 3 types of items:

• format specifiers• white spaces• non-white space characters

int scanf (const char *format [, address, …]);

memory addresses(ie: variables)

There are 2 types of arguments in scanf:

the control string

adapted fromSchildt. C/C++ Programmer’s Reference

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);

This miniLesson is about scanf so that is what we will look at now.

Page 15: This presentation includes custom animations.

15

int scanf( control string, arg1, …, argn);Format specifiersInput 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.

adapted fromSchildt. C/C++ Programmer’s Reference

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

the control string…

Page 16: This presentation includes custom animations.

16

printf(“The answer is %d”, 3);

printf(“She is %d years old.”, age);

printf(“%d + %d = %d”, numA, numB, numA + numB);

Christine S. WolfeOhio University Lancaster2008-Aug-01

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.

1 placeholder 1 value

1 placeholder 1 value

3 placeholder 3 values

Page 17: This presentation includes custom animations.

17

int scanf( control string, arg1, …, argn);Whitespace charactersA 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

Page 18: This presentation includes custom animations.

18

int scanf( control string, arg1, …, argn);Nonwhitespace charactersA 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,20It will fail with 10 20

scanf(“%d %d”, &iAge, &iSize);The reverse is true with the scanf above.It will FAIL on 10,20It will succeed with 10 20

adapted fromSchildt. C/C++ Programmer’s Reference

Page 19: This presentation includes custom animations.

19

int scanf( control string, arg1, …, argn);scansetA 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 CWhen 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

Page 20: This presentation includes custom animations.

20Christine S. WolfeOhio University Lancaster2008-Aug-01

Keyboard Buffer

7 5 . 2 1 81 6 . 5 2\t 3 \n

3 characters are considered "whitespace":• \t (Tab)• \n (newline created by hitting the Enter/Return key)• (a blank space)

There are 4 whitespaces in the keyboard buffer above.

\n

the control string…

Page 21: This presentation includes custom animations.

21Christine S. WolfeOhio University Lancaster2008-Aug-01

Keyboard Buffer

7 5 . 21 6 . 5 2\t 3 \n

scanf treats a set of contiguous whitespace characters as 1 single whitespace.

There are 2 whitespaces in the keyboard buffer above.

\t \t

1 whitespace 1 whitespace

Page 22: This presentation includes custom animations.

22Christine S. WolfeOhio University Lancaster2008-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.

Page 23: This presentation includes custom animations.

23Christine S. WolfeOhio University Lancaster2008-Aug-01

Keyboard Buffer

7 5 . 2 1 8 \n1 6 . 5 2\t 3 \n

scanf("%f" , &AvgAmount);

AvgAmount 16.5

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.

Click forStep 1:

The value is stored in the variable, AvgAmountClick forStep 2:

Keyboard Buffer

7 5 . 2 1 8 \n2\t 3 \n

The stored value is removed from the keyboard buffer and everything moves up. (Notice that the whitespace is still in the buffer.)

Click forStep 3:

Page 24: This presentation includes custom animations.

24Christine S. WolfeOhio University Lancaster2008-Aug-01

The keyboard buffer waits in this state for the next call that accesses the keyboard.

Keyboard Buffer

7 5 . 2 1 8 \n2\t 3 \n

Page 25: This presentation includes custom animations.

25Christine S. WolfeOhio University Lancaster2008-Aug-01

scanf(“\n%d %f" , &ItemCount, &ItemAmount);

This statement says to read a whitespace from the keyboard bufferfollowed by an integer

followed by a whitespacefollowed by a float

and to store the integer in ItemCount and the float in ItemAmount.

Page 26: This presentation includes custom animations.

scanf(“\n%d %f" , &ItemCount, &ItemAmount);

Click for after Step 1:

Click for after Step 2:

Click for after Step 3:

Click for after Step 4:

Keyboard Buffer

7 5 . 2 1 8 \n2 3 \n

ItemCount

ItemAmount

Keyboard Buffer

7 5 . 2 1 8 \n\n

23ItemCount

ItemAmount

Keyboard Buffer

1 8 \n

23ItemCount75.2ItemAmount

Keyboard Buffer

7 5 . 2 1 8 \n2\t 3 \n

ItemCount

ItemAmount

Keyboard Buffer

7 5 . 2 1 8 \n

23ItemCount

ItemAmount

Startingmemory

Page 27: This presentation includes custom animations.

27

Keyboard Buffer

7 5 . 1 8 \n1 6 . 2\t 3 \n

Use fflush(stdin); to empty the keyboard buffer.

5 2

Page 28: This presentation includes custom animations.

28

In this class, unless you have a specific reason to do otherwise,ALWAYS begin your scanf statements as follows:

fflush(stdin);scanf(“\n

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.

Why?

It bears repeating… ALWAYS begin your scanf statements as follows:

fflush(stdin);scanf(“\n

Click Tip

…if you don’t, don’t say you weren’t warned!!!