Display Input and Output (I/O)

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

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

CSE / ENGR 142 Programming I

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

CC112 Structured Programming

Should you know scanf and printf?

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

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

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

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

ET156 Introduction to C Programming

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

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

2. Numbers In, Numbers Out

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

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

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

WARM UP LESSONS BARE BASICS

2. Numbers In, Numbers Out

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.

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

Fundamental of Programming (C)

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

Fundamentals of Programming Session 4

COMP-202: Foundations of Programming

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

Full file at

IT 1033: Fundamentals of Programming Data types & variables

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

CSCI 2132 Software Development. Lecture 8: Introduction to C

Formatted Input/Output

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Programming and Data Structures

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

CS102: Variables and Expressions

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

Getting started with C++ (Part 2)

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

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

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

C Programming Basics

Getting started with Java

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

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

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

Unit 4. Input/Output Functions

C: How to Program. Week /Mar/05

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

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

Programming Language A

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

Basic Types and Formatted I/O

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

Course Outline Introduction to C-Programming

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Programming for Engineers Introduction to C

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

3. Simple Types, Variables, and Constants

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

Fundamentals of Programming

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

ANSI C Programming Simple Programs

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

Chapter 2: Data and Expressions

Fundamentals of C. Structure of a C Program

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

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

Chapter 2 Basic Elements of C++

Introduction to Computing Lecture 03: Basic input / output operations

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

Computers Programming Course 5. Iulian Năstac

Data Types & Variables

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

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

BSM540 Basics of C Language

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

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

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

C Programming

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

بسم اهلل الرمحن الرحيم

CpSc 1111 Lab 4 Formatting and Flow Control

Advanced C Programming Topics

First of all, it is a variable, just like other variables you studied

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

Control Flow, Functions and Basic Linkage

Data types, variables, constants

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

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

Transcription:

2000 UW CSE CSE / ENGR 142 Programming I isplay Input and Output (I/O) -1 Writing Useful Programs 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. -2 What s a Computer? Basic definitions isk (Files) Central Processing Unit Monitor Input: movement of data into memory from outside world (e.g., from keyboard). Changes the value of a variable read operation Main Memory Keyboard Network mouse Output: movement of data from memory to outside world (e.g., to monitor) write operation oes not change value of memory -3-4 ASCII Output Examples of I/O Statements printf("enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; printf("that equals %f degrees Celsius.", celsius); -5-6

isplay 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 REQUIRE for scanf!) -7 printf( ): display output int numpushups; numpushups = 5 ; printf( Hello. o %d pushups.\n, numpushups); output: Hello. o 5 pushups. %d is a placeholder ( conversion character ) for an int value. \n is an escape sequence for newline character. -8 What oes the \n o? int numpushups; numpushups= 5 ; printf( Hello. ); printf( o %d pushups.\n, numpushups); printf( o them now.\n ); output: Hello. o 5 pushups. o them now. Multiple Output Expressions Basic rule: % 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. -9-10 Formatting Output A few of many 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! -11 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 -12

scanf( ): read input scanf ( control string, &input list ) ; int numpushups ; If You Forget the & The program will compile, but when you execute... printf ( Hello. o how many pushups? ) ; scanf ( %d, &numpushups) ; printf ( o %d pushups.\n, numpushups) ; output: Hello. o how many pushups? 5 o 5 pushups. input list variables MUST input list variables MUST be preceded by an &. be preceded by an &. -13-14 Whitespace space ( ), tab ( \t ), newline ( \n ) are whitespace Skipped by scanf for int ( %d ), and double ( %lf ) user can type spaces before a number and they are ignored Not skipped for char input %c each character typed, including spaces, is used 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 studenti ; double grade ; scanf ( %d %lf, &studenti, &grade ) ; -15-16 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 &! I/O Programming Considerations Suppose your program has scanf("%lf", &fahrenheit); and the user types comfortable as the input? --> Nothing is read (so fahrenheit is unitialized) --> Ka-boom This is YOUR problem (not the user s) -17-18

Here s What Happens What You Can o scanf() returns the number of items read successfully int scanfcount; scanfcount = scanf( %d, studenti); /* if scanfcount is not equal to 1 at this point, the user has made some kind of mistake. Handle it. */ -19-20 Option 1: Use the ebugger to Set a Breakpoint -21-22 Execution Pauses at the Breakpoint Option 2: Use assert() Required -23-24

What Happens More Terminology: Syntax vs Semantics/Logic Syntax: the required form of the program punctuation, keywords, word order, etc. The C compiler always catches these syntax errors or compiler errors Semantics and logic: what the program means what you want it to do The C compiler cannot catch these kinds of errors! They can be extremely difficult to find Logic errors may not show up right away -25-26 Syntax or logic errors? Syntax or logic errors? cel = (far-32.0)*5.0/9.0 cel = far-32.0*5.0/9.0; cel = (far-32.0)*5.0/9.0; cel = far-32.0*5.0/9.0; printf ( Celsius is %f, cell); printf ("Celcius is %d", far); printf ( Celsius is %f, cell); printf ("Celcius is %d", far); retrun (0); return (0); retrun (0); return (0); -27-28 I/O 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! Check that you actually read some input! Both: %x s, I/O list match in number, order, type 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 -29-30

Initializing when eclaring int product, i; /*declarations without initializers */ product = 40; i = 5; /*initialization via assignment statements */ int product = 40, i =5; /*declaration with initializers, */ i = 6; /*not an inititialization! */ Initializers are part of the declaration; they are not assignment statements (despite the = sign). 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*/ Q: Where is each of a, b, c, and d initialized? -31-32