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.

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.
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.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include #include int main ( ) { printf( "%d\n", 455 ); printf( "%d\n", 455 ); printf(
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
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.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
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.
 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.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
CSE1301 Computer Programming: Lecture 6 Input/Output.
© 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.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
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.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
Chapter 3: I/O Management
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to C CSE 2031 Fall /3/ :33 AM.
Formatted Input/Output
Plan of the Day: More on type conversions scanf printf format strings
OUTPUT STATEMENTS GC 201.
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.
Formatted Input/Output
Programming in C Input / Output.
Formatted Input/Output
Chapter 9 - Formatted Input/Output
Programming Assignment #1 12-Month Calendar—
Formatted Input/Output
Formatted Input/Output
Formatted Input/Output
EECE.2160 ECE Application Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

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 specifications, which begin with the % symbol. Ordinary characters are printed as they appear in the string; conversion specifications are matched one-by- one with the remaining arguments in the call of printf : int i, j; float x, y; printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y);

The printf Function Warning: C compilers don’t check that the number of conversion specifications in a format string matches the number of output items: printf("%d %d\n", i); /* too few items */ printf("%d\n", i, j); /* too many items */ Furthermore, there’s no check that a conversion specification is appropriate for the type of item being printed: printf("%f %d\n", i, x);

Conversion Specifications A conversion specification has the form %-m.pX m (optional) specifies the minimum field width. - (optional) indicates left justification within the field; the default is right justification..p (optional) specifies: –Number of digits after the decimal point (for a floating point number) –Minimum number of digits to print (for an integer)

Conversion Specifications X is one of the following letters: d - decimal signed integer e - exponential floating point f - fixed decimal floating point g - whichever of e or f produces a shorter string This is not a complete description of conversion specifications.

printf Example Sample program: #include int main(void) { int i; float f; i = 40; f = ; printf("|%d|%5d|%-5d|%5.3d|\n", i, i, i, i); printf("|%10.3f|%10.3e|%-10g|\n", f, f, f); return 0; } Output: |40| 40|40 | 040| | | 8.392e+02| |

Escape Sequences String literals sometimes need to contain control characters (such as tab and newline) or characters that have a special meaning in C (such as "). These characters can be represented by escape sequences. Character escapes consist of \ followed by one character: alert (bell) \a backslash \\ backspace \b question mark \? form feed \f single quote \' new-line \n double quote \" carriage return \r horizontal tab \t vertical tab \v

Escape Sequences Writing the \a escape causes a beep. Writing the \n escape causes the cursor to move to the beginning of the next line. Writing the \t escape causes the cursor to move to the next tab stop. The \" escape allows us to print the double quote character: printf("\"Hello!\""); String literals may contain any number of escape sequences: printf("Item\tUnit\tPurchase\n\tPrice\tDate\n");

The scanf Function scanf requires a format string, followed by variables into which input is to be stored. In most cases, each variable must be preceded by the symbol &. Warning: Forgetting to put the & symbol in front of a variable will have unpredictable — and possibly disastrous — results.

The scanf Function scanf format strings are similar to printf format strings, but usually contain only conversion specifications: int i, j; float x, y; scanf("%d%d%f%f", &i, &j, &x, &y); Note: When used with scanf, the e, f, and g conversions are identical: each causes scanf to read a floating-point number. A sign, exponent, and decimal point may or may not be present.

How scanf Works scanf ignores white-space characters (blanks, tabs, and new-line characters) when searching for an input item. If scanf is used in the following way: scanf("%d%d%f%f", &i, &j, &x, &y); the input could be split over several lines: e3 or put on a single line: e3

How scanf Works When scanf encounters a character that can’t be part of the current item, this character is read again during the scanning of the next input item or during the next call of scanf. Using the same call of scanf as above: scanf("%d%d%f%f", &i, &j, &x, &y); If the input is e3z Then i is assigned the value 1, j is –20, x is.3, and y is –4.0e3.

The scanf Format String A scanf format string may contain ordinary characters in addition to conversion specifications. A non-white-space character in a format string must match the next input character or scanf terminates without reading further. (The non-matching character can be read by a later call of scanf.) The call scanf("%d/%d/%d", &month, &day, &year); will read 5/ 28/ 2002 but not 5 / 28 / 2002

The scanf Format String A white-space character in a format string matches zero or more white-space characters in the input. The call scanf("%d /%d /%d", &month, &day, &year); will read 5 / 28 / 2002 and all similar inputs, regardless of the amount of space before and after each / symbol.

The scanf Format String Warning: Putting a new-line character at the end of a scanf format string is usually a bad idea: scanf("%d\n", &i); After reading an integer, scanf skips white-space characters until it finds a non-white-space character. An interactive program will hang until the user enters a nonblank character.