Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPC144 Introduction to Programming Using C Week 8 – Lesson 1

Similar presentations


Presentation on theme: "IPC144 Introduction to Programming Using C Week 8 – Lesson 1"— Presentation transcript:

1 IPC144 Introduction to Programming Using C Week 8 – Lesson 1
(Pages 60 to 66 in IPC144 Textbook)

2 Agenda Additional C programming Elements
A Detailed Look at scanf() Function Better Output Control with printf() Function Exercises / Examples

3 scanf() Function To write programs that handle input well, we will have to learn more about how the scanf() function works. This will prevent confusion when writing programs that accept BOTH numbers and characters…

4 scanf() Function As you recall, the first parameter is the format specifier indicating the data-type of the user’s input, and the second argument is a pointer to the variable’s memory address location. You can also combine multiple input within one scanf() statement…

5 scanf() Function For Example: scanf(“%d,%d”, &x, &y); This scanf statement will allow the user to enter an int followed by a comma, followed by another int. The values separated by a comma get stored into the variables x and y respectively… The comma character is considered to be IGNORED.

6 scanf() Function There are two exceptions to this scanf() rule previously mentioned: If the scanf statement contains format specifier “ %c” (i.e. a space before %c), then any whitespace characters and newline characters are ignored. Useful if ints were previously scanned and \n symbol remains… If there is an asterisk (*) between % and format specifier, then scanf will ignore that particular data-type. Eg. scanf (“%*c%c”, & letter); will ignore the first character, and place the next character in the variable called letter….

7 scanf() Function Note:
If scanf() runs out of input before the end of the format specifier string, it will simply stop and wait for another line of input. If scanf() receives too much input that the format specifier string, then the left-over input is kept around for the next scanf(). This is the reason why we should use “ %c” to prevent this extra input interfering with we want input to be actually scanned.

8 PRACTICE REALITY CHECK (Week 8 – Lesson 1) Question 1 (Walk-thru)

9 PRACTICE REALITY CHECK (Week 8 – Lesson 1) Question 2 (Word Question)

10 Checking scanf() Success
It is possible to see if scanf() function found the kind of data it was looking for. This is useful for error-checking… scanf() is a function that returns a value: If scanf() was successful, it returns the number of variables that it actually filled with input data. If scanf() was unable to fill the first variable, it would return a value of zero or possibly -1

11 Clearing scanf() Buffer
Sometimes, it is necessary to clear the scanf() buffer to prevent any complications in your program when scanning for input. We have learned to use “ %c” as a format specifier to clear any remaining data. But there is another method…

12 Clearing scanf() Buffer
You can create a function called “clearInput()” that uses a function to read characters and “use them up” to clear the buffer. The getchar() function is another method of obtaining a character (other than scanf()…

13 Clearing scanf() Buffer
Eg. void clearInput() { while (getchar() != ‘\n’ ) ; /* i.e. do “nothing” */ } The clearInput() function can be called after scanf() function to act to “clear any characters” from buffer…

14 PRACTICE REALITY CHECK (Week 8 – Lesson 1) Question 3 (Word Question)

15 Output Control with printf()
We have seen how display decimal places can be specified in a printf() statement involving double data-types. You can also control the display the width of the data value.

16 Output Control with printf()
Rules: The width of a data-type field appears as a number between % and the data type specifier. If decimals are to be displayed, then the number appears between the % and the decimal point number, then followed by the data type specifier

17 Output Control with printf()
Rules / continued: If the number if positive, then leading spaces will be added. Assume d is 15 and c is ‘x’… e.g. printf (“ \n”); printf (“%5d%4czzz\n”, d, c); OUTPUT xzzz Leading spaces added to right-align d and c

18 Output Control with printf()
Rules / continued: If the number is negative, then trailing spaces will be added. e.g. printf (“ \n”); printf (“%-5d%-4czzz\n”, d, c); OUTPUT x zzz Trailing spaces added to left-align d and c

19 PRACTICE REALITY CHECK (Week 8 – Lesson 1) Question 4 (Walk-thru)

20 Homework TASK #1 TASK #2 TASK #3 TASK #4 *** Highly Recommended ***
Complete lab #5 since it is due at the beginning of this week’s lab! TASK #2 Make certain to get your marked Test #1 in class, and view the solutions in week #8 notes… TASK #3 Read IPC144 Programming Notes (Filling in the Gaps). TASK #4 *** Highly Recommended *** Read ahead in IPC144 Programming Notes (Arrays).


Download ppt "IPC144 Introduction to Programming Using C Week 8 – Lesson 1"

Similar presentations


Ads by Google