Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
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.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
 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.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
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.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
Chapter 3 Input and Output
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Topics to be covered ä ä Program Structure ä ä Constants ä ä Variables ä ä Assignment Statements ä ä Standard Output ä ä Standard Input ä ä Math Functions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
CS 1704 Introduction to Data Structures and Software Engineering.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Chapter 9 C Formatted Input/Output
Input/output.
TMF1414 Introduction to Programming
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Programming in C Input / Output.
Input and Output Lecture 4.
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.
Chapter 08- printf and scanf
Programming in C Input / Output.
C Formatted Input / Output Review and Lab Assignments
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 9 - Formatted Input/Output
Programming in C Input / Output.
Introduction to C EECS May 2019.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 18 I/O in C.
Presentation transcript:

Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions

Standard Output #include ä ä printf(“format string”, arg1, arg2,.... argN); // to the screen ä ä fprintf(file_ptr, “format string”, arg1, arg2,.... argN); // to a file ä ä sprintf(str_ptr, “format string”, arg1, arg2,.... argN); // to a string ä ä each returns the number of characters printed. Format String ä ä All but two characters printed verbatim. ä ä % character used to format and output the next argument. ä ä \ character used to print characters that can’t be typed directly. ä ä Must be at least as many arguments as % specifiers.

% - printf() conversion specifiers Format: %[flags][width][.precision][modifier]type_character ä ä All except type_character are optional. ä ä Conversion specifier is everything from % sign to the first type_character. ä ä [flags] - controls justification, leading spaces, sign, etc. ä ä {-, +,, #, 0} ä ä [width] - sets the minimum width of the field - may be longer. ä ä [.precision] - sets the number of digits following the decimal point. ä ä Usually used for floating points, but also affects character and integer types as well. ä ä [modifier] - combines with type_character to determine argument type. ä ä type_character - the basic type of that argument. ä ä {c, d, e, E, f, g, G, i, o, p, s, u, x, X, %} In general, look up what you need. You will tend to remember the forms you use most often.

\ - printf() escape sequences Format: \(character) or \(number) ä ä If a character follows the \, the action indicated by the character is performed. ä ä \n - newline ä ä \r - return w/o line feed. ä ä \” - print double quote ä ä \’ - print single quote (aka apostrophe) ä ä \a - sound bell ä ä \b - backspace ä ä \\ - print backslash ä ä \? - question mark ä ä If a number follows the \, the ASCII character of the octal number is printed.

Standard Input #include ä ä scanf(“format string”, arg1, arg2,.... argN); // from the keyboard ä ä scanf(file_ptr, “format string”, arg1, arg2,.... argN); // from a file ä ä sscanf(str_ptr, “format string”, arg1, arg2,.... argN); // from a string ä ä each returns the number of successful conversions. Format String ä ä Literal characters in format string can be used to skip characters. ä ä % conversion specifiers similar (but not identical) to printf(). ä ä Arguments need to be memory locations where values will be stored. Big Time Caveat ä ä If input does not adequately match format, results can be VERY unpredictable. ä ä Many programmers avoid the use of scanf() at nearly any cost. ä ä Use of fscanf() for file I/O is generally safe provided the file format is adequately constrained.

Common mistakes with scanf() Passing values instead of memory locations. ä ä The scanf() function read values and store them at the memory locations you supply. k = 10; scanf(“%i”, k); ä ä Tells scanf() to format the value as an integer and store it at location 10. ä ä But the memory location for variable k is almost certainly not 10. ä ä The address operator, &, returns the memory location of the specified variable. scanf(“%i”, &k); ä ä Tells scanf() to format the value as an integer and store it at the memory address used for variable k. Using %lf for doubles. ä ä printf() uses %lf for both floats and doubles because the compiler promotes all arguments of type float to double. ä ä scanf() cannot due this. It must know which type the variable is so that the number and format of the bytes stored is correct.