CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Similar documents
Display Input and Output (I/O)

CSE / ENGR 142 Programming I

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

CC112 Structured Programming

Should you know scanf and printf?

ET156 Introduction to C Programming

COMP-202: Foundations of Programming

WARM UP LESSONS BARE BASICS

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Unit 4. Input/Output Functions

Introduction to Computing Lecture 03: Basic input / output operations

2. Numbers In, Numbers Out

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Advanced C Programming Topics

Programming Language A

Course Outline Introduction to C-Programming

2. Numbers In, Numbers Out

Fundamental of Programming (C)

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

Formatted Input/Output

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Getting started with C++ (Part 2)

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSCI 2132 Software Development. Lecture 8: Introduction to C

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

CS16 Exam #1 7/17/ Minutes 100 Points total

Full file at

IS12 - Introduction to Programming. Data Types and Variables

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Lesson 7. Reading and Writing a.k.a. Input and Output

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Fundamentals of Programming Session 4

C: How to Program. Week /Mar/05

Programming and Data Structures

C Programming

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Visual C# Instructor s Manual Table of Contents

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

C Programming Basics

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

For instance, we can declare an array of five ints like this: int numbers[5];

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

Chapter 11 Introduction to Programming in C

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Fundamentals of Programming

ET156 Introduction to C Programming

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Getting started with Java

CSE 303 Lecture 8. Intro to C programming

The C++ Language. Arizona State University 1

CpSc 1111 Lab 4 Formatting and Flow Control

CS102: Variables and Expressions

Input/Output, Libraries, and Files 2000 UW CSE

Control Flow, Functions and Basic Linkage

Arrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays

2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park

BSM540 Basics of C Language

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Programming for Engineers Introduction to C

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

CSE 374 Programming Concepts & Tools

Data types, variables, constants

printf( Please enter another number: ); scanf( %d, &num2);

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Data Types. Data Types. Integer Types. Signed Integers

Full file at

ANSI C Programming Simple Programs

Chapter 2: Data and Expressions

Chapter 11 Introduction to Programming in C

IT 1033: Fundamentals of Programming Data types & variables

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Introduction to Programming

Transcription:

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) Overview (5) Topics Output: printf Input: scanf Basic format codes More on initializing variables 2000 UW CSE E-1 E-2 Writing Useful Programs What s a Computer? It s hard to write useful programs using only variables and assignment statements Even our Fahrenheit to Celsius program needed more: Needed a way to get data into and out of the program We ll learn more about doing this today Lots of terminology and messy details, but worthwhile. E-3 Disk (Files) Central Processing Unit Main Memory Monitor Keyboard Network mouse E-4 E-1

Basic Definitions Text Output Input: movement of data into memory from outside world (e.g., from keyboard). Changes the value of a variable read operation Output: movement of data from memory to outside world (e.g., to monitor) write operation Does not change value of memory E-5 E-6 I/O Statements from a Familiar Program printf("enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; printf("that equals %f degrees Celsius.", celsius); E-7 Display Input and Output The functions printf and scanf provide basic display I/O services. printf( control string, list of expressions) ; scanf( control string, list of &variables) ; Control string gives the format of output or input. Expressions are what to output. Variables are where to store the input. & is magic (that is REQUIRED for scanf!) E-8 E-2

printf( ): Display Output What Does the \n Do? int numpushups; numpushups = 5 ; printf( Hello. Do %d pushups.\n, numpushups); output: Hello. Do 5 pushups. %d is a placeholder ( conversion character ) for an int value. \n is an escape sequence for newline character. E-9 int numpushups; numpushups= 5 ; printf( Hello. ); printf( Do %d pushups.\n, numpushups); printf( Do them now.\n ); output: Hello. Do 5 pushups. Do them now. E-10 Getting a Little Fancier printf( control string, list of expressions) ; printf might have more than one expression in its list: printf( %d times %f is %f. \n, multiplier, pi, (double) multiplier * pi) E-11 Multiple Output Expressions % placeholders in format string match expressions in output list in number, order, and type. int multiplier; double pi; pi = 3.14; multiplier = 2; printf( %d times %f is %f. \n, multiplier, pi, (double) multiplier * pi ); Output: 2 times 3.14000 is 6.28000. E-12 E-3

Advanced Output Formatting This is only the beginning! A few of many other things you can do: Control number of decimals 3.1 vs 3.100000 Exponential (scientific) or decimal notation 3.1 vs 3.1E0 Control total width (including spaces) 3.1 vs 3.1 How? E-13 Advanced Output Formatting This is only the beginning! A few of many other things you can do: Control number of decimals 3.1 vs 3.100000 Exponential (scientific) or decimal notation 3.1 vs 3.1E0 Control total width (including spaces) 3.1 vs 3.1 How? Look in textbook or a reference manual, or online help! E-14 Output Format Examples %10.2f 1 2 3. 5 5 double %10.4f 1 2 3. 5 5 0 0 %.2f 1 2 3. 5 5 %10d _ 4 7 5 int %-10d 4 7 5 _ %10c _ a char E-15 scanf( ): Read Input scanf ( control string, &input list ) ; int numpushups ; printf ( Hello. Do how many pushups? ) ; scanf ( %d, &numpushups) ; printf ( Do %d pushups.\n, numpushups) ; output: Hello. Do how many pushups? 5 Do 5 pushups. input list variables MUST be preceded by an &. input list variables MUST be preceded by an &. E-16 (Did I already say that?). E-4

If You Forget the & The program will compile, but when you execute... If You Forget the & The program will compile, but when you execute... E-17 E-18 **Whitespace (start 6 Oct) space ( ), tab ( \t ), newline ( \n ) are whitespace Whitespace is skipped by scanf for int ( %d ), and double ( %lf ) This means the user can type spaces before a number and they are ignored Not skipped for char input %c each character typed, including spaces, is used E-19 Multiple Inputs Basic rule: % placeholders in the format must match variables in the input list MUST! match one-for-one in number, order, and type. int studentid ; double grade ; scanf ( %d %lf, &studentid, &grade ) ; E-20 E-5

Format Items Summary Type scanf() printf() char %c %c int %d %d %i also works double %lf %f (long) float What happens if types don t match? printf -- garbled output scanf -- unpredictable errors and don t forget the &! E-21 printf/scanf Summary Output: printf( control string, output list); output list expressions; values to be printed control string types and desired format for now, NO &, ever! Input: scanf( control string, &input list); input list variables; values to be read control string types and expected format can be a way of initializing variables for now, YES &, always! Both: %x s, I/O list match in number, order, type E-22 I/O Summary Input is the movement of data into memory In C, we use scanf for input from the keyboard Output is the movement of data from memory In C, use printf for output to the screen Know the basic printf/scanf rules, and know them well Be aware that advanced formatting options exist and can be looked up when needed E-23 Bonus Topic: More on Initializing Variables Review: Initialization means giving something a value for the first time. Potential ways to initialize: Assignment statement scanf Yet another way: initializer with declaration E-24 E-6

Initializing when Declaring Declarations without initializers int product, i; product = 40; i = 5; Declarations with initializers int product = 40, i =5; i = 6; Initializers are part of the declaration; they are not assignment statements (despite the = sign). E-25 Initialization Quiz int main (void) { /*line 1*/ int a, b, c, d=10; /*line 2*/ b=5; /*line 3*/ d=6; /*line 4*/ scanf("%d %d", &b, &c); /*line 5*/ return 0; /*line 6*/ } Q: Where is each of a, b, c, and d initialized? E-26 Next Time We ll learn about a powerful new type of statement, the conditional or if statement E-27 E-7