Chapter 08- printf and scanf

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.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
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.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
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.
 Pearson Education, Inc. All rights reserved Formatted Output.
 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.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
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.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
© 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.
CS 1704 Introduction to Data Structures and Software Engineering.
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.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 9 C Formatted Input/Output
ECE Application Programming
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
Unit-4, Chapter-2 Managing Input and Output Operations
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan of the Day: More on type conversions scanf printf format strings
Chapter 2 - Data Types and Storage Classes
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.
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
Formatted Input/Output
A First Book of ANSI C Fourth Edition
Chapter 9 - Formatted Input/Output
Programming in C Input / Output.
Formatted Input/Output
Conversion Check your class notes and given examples at class.
Formatted Input/Output
Introduction to Java Applications
Formatted 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.
Presentation transcript:

Chapter 08- printf and scanf format indicators advanced format control printf scanf

printf revisited general form: printf(“format string”,arg0,arg1,...,argn); “format string” contains literal text and format indicators (signalled by % sign). formatters and arguments are matched from left. printf does not automatically insert a new line. Normally doesn’t display any data until newline is issued by program!

Common printf errors use the wrong format indicator: e.g. use a %d with a float variable (what happens?) use the wrong number of formatters: too few formatters - rightmost argument(s) will be ignored too many formatters - garbage is printed in the place(s) given by the rightmost formatter(s)

Integer formatters %d signed decimal %i signed decimal (not really necessary) %u unsigned decimal %o unsigned octal %x unsigned hexadecimal (w/ lower case a-f) %X unsigned hexadecimal (w/ upper case A-F)

notes on integer formatters By default, leading zeros are not printed “0x” is not printed for hexadecimal chars and shorts are promoted to full ints if displayed via an integer formatter width of field varies depending on size of integer %ld, %lu, ... for long ints (the ‘l’ is a modifier, as we will see soon)

Real number formatters %f real number [-]mmm.dddddd (6 fraction digits) %e scientific form [-]m.dddddd e[+/-] xx %E scientific form [-]m.dddddd E[+/-] xx 7 digits total, with one before decimal point %g general purpose formatter (real or scientific) %G General purpose formatter (real or scientific) will use %f unless value <1.e-04 or >=1.e+07 in which case it uses %e for %g or %E for %G. %Lf, %Le, %Lg for long doubles

Other formatters %c single character %s string (print until \0 hit) %n prints total number of characters printed with printf to this point %p prints pointer/address (machine dependent) %% just prints a % sign

Advanced control of printf format The general form of a formatter is: %[flag][width][.precision][modifier]format indicator All [fields] are optional flag controls justification and other properties Width gives total length of display field precision gives # characters in precision field modifier short, double, and long control

Format flags Flag Operation Default 0 leading zeros leading spaces - Output is left justified Right justified + Always display sign only - sign <space> prints space or minus sign only - sign Note: + overrides <space> if both are given # adds leading 0 to octal format adds 0x (0X) to %x (%X) format forces decimal point in real formats (even if integer)

Field width specifier Insert total number of characters: printf(“%20s”,st1); /* yields a width of 20 */ the field width is overridden if it is too small to accurately display variable. adjustable field width is indicated by an * you must give an argument in the list before the variable to give the (integer) length printf(“%*s”,20,st1); /* equivalent to 1st example */

.precision specification Indicated by a leading period: %.5f %f, %e indicates # of digits to right of . %g indicates total # digits in mantissa %d, ... indicates minimum # digits printed %s indicates maximum # of characters displayed (the rest is truncated) %c, %p no effect

Precision comments When used with field width: %12.5f (field width is NOT necessary) Variable precision printf(“%.*f”,num_prec,num); printf(“%*.*f,num_width,num_prec,num);

Program 8.1 #include <stdio.h> #define PI 3.141592654f int main(void) { /*demonstrates advanced formatting */ int i; char test[]="Hi World"; for (i=1;i<16;i++) { printf("%*s %+0*f\t\t",i,test,i,PI); printf("%.*s %+0.*f\n",i,test,i,PI); } return 0;

final printf comments type modifiers returned value h short integer l long integer L long double h modifier exists with printf only for symmetry with scanf; short ints automatically promoted to int. returned value printf returns an int with the number of characters successfully printed or EOF (usually -1) for an error

scanf revisited scanf(“format string”,&arg1,...,&argn); argument addresses must be given returns an int of the # of successful conversions made (NOT # characters) or EOF (usually -1) if there is an error The burden of insuring correct input is up to user. Initial white space ignored, but separates fields. scanf stops as soon as data is read or error occurs. Any and all unread characters remain for next scan!

Advanced control of scanf format many of the formatters are identical with printf formatters. We will only describe differences. %i will recognize any integer format (dec, oct, hex) %hd must be used to read short ints %f is only for floats %lf is for doubles /* common source of errors */

Effect of literal text in format Except for white space, any literal text in format string must be matched exactly by input source or scanf aborts: scanf(“Radius = %lf ”,&radius); Sometimes useful for disk file input, but there is typically a better way to ignore some input

The %* format indicator Inserting an asterisk between % and any specifier tells scanf to ignore that entry: scanf(“%*s, %lf”,&radius); is better than previous alternative

Matching character sets one can replace %s with a text string that contains a subset of all characters. scanf stops reading when any character outside that string is encountered. scanf(“%[abcdefghijklmnopqrstuvwxyz]”,name); reads only lower case letters can be used to read spaces can be abbreviated with hyphen scanf(“%[a-zA-Z 0-9]”,name);

Excluding character sets to exclude characters, use: %[^char set] To reject lower case letters use: scanf(“%[^a-z]”,name); to accept all characters, use scanf(“%[^\n]”,name);

Field width specification Field width, but not precision, can be specified with the same approach as printf: scanf(“%4d %6f”,&i, &x); This fixed format input is most useful with disk data.

Example 8.2 #include <stdio.h> /*********************************** * * * Demonstration of scanf formatting * * History : Date Reason * * 12/01/14 Created by W. Lawson * ***********************************/ int main(int argc, char *argv[]) { char name[80]; int test; printf("Input a string after each prompt\n\n"); printf("First, an unformatted scanf\n\n"); scanf("%s",name); printf("Result: %s\n\n",name); fflush(stdin);

Ex 8.2 continued printf("Now, with: scanf(\" %[^\\n] \",name);\n\n"); scanf("%[^\n]",name); printf("Result: %s\n\n",name); fflush(stdin); printf("Now, with: scanf(\" %[^a-z] \",name);\n\n"); scanf("%[^a-z]",name); printf("Now, with: scanf(\" %[a-z WL] \",name);\n\n"); scanf("%[a-z WL]",name);

Ex 8.2 continued printf("Now, with: scanf(\" %[a-zWL] \",name);\n\n"); scanf("%[a-zWL]",name); printf("Result: %s\n\n",name); fflush(stdin); printf("Now, with: scanf(\" %%*s, %%i \"&test);\n\n"); scanf("%*s, %i",&test); printf("Result: %i\n\n",test); return 0; }