CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.

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

Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
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.
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.
1 CSE1301 Computer Programming: Lecture 9 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.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
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.
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.
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.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 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.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Dale Roberts Basic I/O (Chap. 9) CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
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.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
CSE1301 Computer Programming: Lecture 6 Input/Output.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
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.
CS 1704 Introduction to Data Structures and Software Engineering.
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.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Input & Output Operations Week 6 SCP1103 Basic C Programming SEM1 2010/2011.
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 3: I/O Management
Plan of the Day: More on type conversions scanf printf format strings
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.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 9 - Formatted Input/Output
Department of Computer and Information Science
Conversion Check your class notes and given examples at class.
Introduction to Java Applications
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004

Input/Ouput (i/o) in C C has extensive facilities for input and output (i/o) processing. unlike most programming languages the i/o facilities are not part of the core language but part of the standard library. The i/o facilities are character and line i/o formatted i/o (and string versions) formatted file i/o low-level file i/o First we focus on formatted i/o and the library functions printf() and scanf(). You will need to include the header file stdio.h.

printf() printf() (print formatted) generates output under the control of a format string (its first argument) which consists of literal characters to be printed and also special character sequences called format specifiers (conversions). Conversions request that other arguments be inserted into the string. Format printf(format-control-string, other-arguments); format-control-string describes output format other-arguments correspond to each conversion specification in format- control-string Each specification begins with a percent sign %.

Format Specifiers for printf() Here are format specifiers for printf(): %dprint an int argument in decimal %ldprint a long int argument in decimal %cprint a character %sprint a string %fprint a float or double argument %esame as %f, but use exponential notation %guse %e or %f, whichever is better %oprint an int argument in octal (base 8) %x or %Xprint an int argument in hexadecimal (base 16) %print a single %

Format Specifiers for printf() (2) %g prints with no trailing zeros ( becomes 1.23). %E and %G are similar to %e and %g respectively but they display a capital E when printing in exponential format Here are some other format specifiers that are part of the standatd but they are rarely used: %n corresponding argument must be of type *int, into which gets stored the number of characters read so far. Nothing is actually printed. %p corresponding argument must be of type *void, allows you to display a memory address (in hexadecimal format).

Field Width and Precision specify the width and precision of numbers as they are inserted Field width- size of field in which data is printed. (Minus sign uses one character position). If width larger than data, data is right-justified by default; if field width too small it increases to fit data. Width inserted between % and conversion specifier. Example: %4d. Floating point precision- number of digits to appear after decimal (e and f specifier). Format: A dot. followed by precision after the % character, e.g. %.3f specifies a precision of 3. Field width and precision can both be specified, %width.precision, e.g. %5.3f

Flags Flags supplement formatting capabilities Place flag immediately to the right of percent sign Here are two commonly used flag: - converted argument left-justified (default is right justification) 0zeros used instead of spaces to pad a field Example: int i = 123; printf("%05d %-5d", i, i) will display " "

scanf() Formatting input with scanf() function is used to read information from the keyboard. Format: scanf(format-control-string, other-arguments); format-control-string describes formats of inputs other-arguments - list of pointers to variables where input will be stored. The scanf() needs to know the address of the variables it is using. A scanf() does not display anything, the control string is only for the conversion character representing the variable you want to store.

Format Specifiers for scanf() Conversions (format specifiers) similar to printf(), notice though that double is different. %ccharacter %dinteger %ooctal integer %x or %X hexadecimal integer %ffloating point number %lfdouble floating point number %sstring of characters NOTE: Users must press enter key after they type their input.

scanf() continued. The control string may also contain white space - matches white space in input stream. Note if several consecutive whitespace characters appear in the control string, the effect is the same as if only one had appeared. ordinary characters (other than % or format specifier) - must match input stream exactly. Return values: printf() returns the number of characters printed. scanf() returns the number of things read, e.g.: count = scanf("%d%d",&x,&y);

The scanf() function is notoriously problematic and some C programmers don't use it at all, preferring character and line input (see later)!