I/O with scanf()/printf() I/O stands for “Input/Output” In CSE 142, you will use: –scanf() (reading from the keyboard) –printf() for output (printing to.

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
 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.
CSC 200 Lecture 2 Matt Kayala 1/25/06. Lecture Objectives More detailed intro to C++ Variables, Expressions, and Assignment Statements Console Input/Output.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© 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.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
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.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
ITM © Port, KazmanVariables - 1 ITM 352 Expressions, Precedence, Working with Strings.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Slide 1. Slide 2 Chapter 1 C++ Basics Slide 3 Learning Objectives  Introduction to C++  Origins, Object-Oriented Programming, Terms  Variables, Expressions,
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.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
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.
Lecture2.
Formatted Input and Output
Input/output.
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
ITM 352 Expressions, Precedence, Working with Strings Class #5
Conversions of the type of the value of an expression
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to CS Your First C Programs
Lecture3.
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

I/O with scanf()/printf() I/O stands for “Input/Output” In CSE 142, you will use: –scanf() (reading from the keyboard) –printf() for output (printing to the screen) Side note: scanf() and printf() are functions, which you’ll learn about later in the course –“printf” == “print function” –“scanf” == “scan function”

The format for scanf()/printf() Both scanf() and printf() follow the same format: they take a literal string describing the format of the input or output, optionally followed by a list of arguments (comma-seperated list of expressions) The literal string (what’s between the double-quotes “ ”) is read/displayed exactly as it is typed by the programmer

Escape sequences printf(“Farewell, cruel world!”); –Prints Farewell, cruel world! exactly as typed “Exactly as typed”? What if I wanted to print –a new line? a tab character? –the value of a variable? Printing special characters (ie - non-printing characters) such as a newline or a tab require escape sequences - character codes which represent them Two useful escape sequences: –\n - newline –\t - tab

The placeholders A placeholder: tells the compiler “I want to {read into/print} a variable, but I’m not sure of its value right now. Save me some space, and I’ll {get/tell you} its value at runtime” –Placeholders look like percent signs followed by a code describing the type of the variable Placeholders are placed into the literal string (ie - between the “ ”), and must have a variable or expression which matches in type in the argument list intdoublechar scanf()%d%lf%c printf()%d%f%c

printf() and scanf() in detail int i = 10; char c; double d; printf(“Hello World!\n”); printf(“This is a number: 9\n”); Printf(“This is the name of a variable: d\n”); printf(“Enter a number: ”); scanf(“%lf”, &d); printf(“This is the value of a variable: %f\n”, d); Non-printing character Display a prompt scanf does the actual reading of the input These match in number and type

Ints and Doubles and Chars … All variables in C have a type Review: We’ve seen int, double, and char so far What is the type of an expression made from variables of the same type? /* No surprises so far */ * 7 2 / 7 2 % * / 6.2

… Oh My! Dilemma: –What if you had an expression made from variables of differing types? * *

A Golden Rule One of the many golden rules to learn in CSE 142: –When evaluating expressions, data is never lost! * * An int and a double or Which of the above is more precise?

Division: It’s why we care In other words, any expression involving a double evaluates to a double Why is the type of an expression such a big deal? –Both of these expressions evaluate to 9 or What is the value of the following two expressions? 2 / / 7.0 The moral of the story: –Be aware of your types!

What’s really happening When an expression involving mixed types is being evaluated: –The type of the result is the type of the most precise type –The operands are cast (that is, converted) to the resultant type before the expression is evaluated aDouble = 4 * 5.2; The most precise type is a double, so the entire expression evaluates to a double This 4 is cast to a double

Explicitness is a good thing … In the previous example, aDouble = 4 * 5.2; 4 was implicitly cast to a double You can also explicitly cast your variables! An explicit cast has the following form: ( type ) expression; Examples: (double) anInt; 2 + (int) 4.5; (double) 6 * myInt; Like implicit casts, explicit casts are always done first!

Why explicitness is good Explicit casts guarentee that the correct operator is being called (double) 2 / 7 Explicit casts make it clear to readers (ie -- your TA’s!) what you intend NB: Parentheses matter! (double) (2 / 7) “NB” == “Nota Bene” == Latin for “Note this well” That is, “Remember this!”

Casting Assignments Casting also occurs during assignment statements if the type of the assigned variable differs from the type of the expression anInt = 4 * 5.2; The most precise type is a double, so the entire expression evaluates to a double But the variable being assigned to is an int ! So the value assigned into anInt is an int