IPC144 Introduction to Programming Using C Week 8 – Lesson 1

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Introduction to C Programming
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Introduction to C Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Strings CSCI 112: Programming in C.
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
© 2016 Pearson Education, Ltd. All rights reserved.
Input/output.
Revision Lecture
Unit-4, Chapter-2 Managing Input and Output Operations
Pointers.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatted Input/Output
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
INPUT & OUTPUT scanf & printf.
Week 9 – Lesson 1 Arrays – Character Strings
IPC144 Week 10 – Lesson 2 Working with Files
Chapter 9 - Formatted Input/Output
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Conversion Check your class notes and given examples at class.
Chapter 16 Pointers and Arrays
Formatted Input/Output
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Week 9 – Lesson 2 Input & Output of Character Strings
Formatted Input/Output
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
EECE.2160 ECE Application Programming
Introduction to C EECS May 2019.
Variables in C Topics Naming Variables Declaring Variables
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Introduction to C Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Getting Started With Coding
Presentation transcript:

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

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

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…

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…

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.

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

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.

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

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

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

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…

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()…

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…

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

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.

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

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 (“123456789012345\n”); printf (“%5d%4czzz\n”, d, c); OUTPUT 123456789012345 15 xzzz Leading spaces added to right-align d and c

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

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

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