16.216 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 6: printf() formatting.

Slides:



Advertisements
Similar presentations
printf() Documentation info:
Advertisements

ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
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.
 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",
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.
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.
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.
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
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.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
CS 125 Lecture 9 Martin van Bommel. Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1,
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.
+ 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.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
ECE Application Programming
Formatted Input and Output
ECE Application Programming
Chapter 9 C Formatted Input/Output
ECE Application Programming
ECE Application Programming
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
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
Lecture 13 Input/Output Files.
Formatted Input/Output
Chapter 9 - Formatted Input/Output
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Chapter 4 Managing Input and Output Operations
EECE.2160 ECE Application Programming
Formatted Input/Output
Conversion Check your class notes and given examples at class.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Formatted Input/Output
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
EECE.2160 ECE Application Programming
Presentation transcript:

ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 6: printf() formatting

Lecture outline Announcements/reminders  Program 2 due Wednesday, 2/6  Program 3 to be posted; due 2/13 Today’s class  printf() formatting  Operators 5/15/2015 ECE Application Programming: Lecture 6 2

Formatted output: field width Specifying field width (min # characters) (assume n = 12):  printf(“%10d”, n);  12 (8 spaces, then number 12) To left justify value in field, use – flag before width  printf(“%-10d”, n);  12________ (Number 12, then 8 spaces) To force the sign to show, use + flag before width  printf(“%+10d”, n);  +12 (7 spaces, a plus sign, then number 12) To place 0s before the value, use 0 flag before width  printf(“%010d”, n);   printf(“%+010d”, n);   printf(“%-010d”, n);  12________ To use a variable to specify field width, use * as the width  printf(“%*d”, n, n);  12 (Field width = 12  10 spaces, then the number 12) Field width never truncates  printf(“%1d”, n);  still prints 12 5/15/2015 ECE Application Programming: Lecture 4 3

Formatted output: precision For examples, int n = 12; double x = Specifying precision:  # chars after decimal point for FP (%f, %lf) Rounds last digit printf(“%.6lf”, x);  printf(“%.1lf”, x);  3.8 printf(“%.0lf”, x);  4  Minimum # chars for integer (%d, %i, %u, %x) Does not truncate; will pad with leading 0s printf(“%.1d”, n);  12 printf(“%.3d”, n);  012  Max # chars for string (%s) printf(“%.5s”, “one”);  one printf(“%.5s”, “one two”);  one t  No effect for char (%c) As with field width, can use * to specify that field width is a variable 5/15/2015 ECE Application Programming: Lecture 4 4

Formatting and scanf() Formatting specified above used only for printf() Have very different meaning for scanf()  printf("%3d", x)  print x w/field width of 3  scanf("%3d", &x)  read 3 characters into x If you enter: 12  x = 12 If you enter: 1234  x = 123 Bottom line: you probably don’t want to “format” your input! 5/15/2015 ECE Application Programming: Lecture 4 5

Note The next three slides contain full specification for printf() flags, field width, and precision Read on if you want lots of details; skip these slides if the previous ones were sufficient 5/15/2015 ECE Application Programming: Lecture 4 6

7 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type flagmeaningdefault -Left align the result within the given field widthRight align +Prefix the output value with a sign (+ or -) if the output value is of a signed type. Sign appears only for negative signed values (-) 0If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and - appear, the 0 is ignored. No padding (actually space padding) blankPrefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears #When used with the o, x, or X format, the # flag prefixes any nonzero output with 0, 0x, or 0X, respectively. Ignored when used with c, d, i, u, or s. No prefix #When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases. Decimal point appears only if digits follow it. #When used with g or G format, forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros. Decimal point appears only if digits follow it. Trailing zeros are truncated. This slide adapted from information in MSDN Library 5/15/2015 ECE Application Programming: Lecture 4

8 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. Notes: If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). This slide adapted from information in MSDN Library 5/15/2015 ECE Application Programming: Lecture 4

9 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type typmeaningdefault cThe precision has no effectcharacter is printed d,i, u,o, x,X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision Default precision is 1. e,EThe precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6; if precision is 0 or the period fThe precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. (.) appears without a number following it, no decimal point is printed. g,GThe precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated. s,SThe precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered. This slide adapted from information in MSDN Library 5/15/2015 ECE Application Programming: Lecture 4

10 examples - printf() float a=67.49,b= ; printf("Price:█%10f█%10f\n",a,b); printf("%8.2f█%8.4f█%5.1f█%7.5f\n",a,a,b,b); printf("a=%5.5f,█b=%0.2f\n",a,b); Printed: print position Price:██ ███ columns ███67.49██ ██10.0█ to 35 a= ,█b=10.00 Note: On last line of output, the actual value was output; the printf routine overrode the specified width. 5/15/2015 ECE Application Programming: Lecture 4

11 examples - printf() float a=67.49,b= ; int i=184,j=-51; double x= , y=-22.33; printf("%7.1lf%5d%8.2f\n",y,i,a); printf("%13.2f%4d%9.2lf\n",b,j,x); //changed Printed: print position columns to 35 Notes: d indicates decimal int eger f indicates float lf (that’s lower case L and f) indicates double (for double output, f OK too; not OK for input) 5/15/2015 ECE Application Programming: Lecture 4

12 printf() examples printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78);..../ / / /<= ruler vv-12ww-34xx-56yy-78zz printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78);..../ / / /<= ruler vv 12ww34xx+56yy78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78);..../ / / /<= ruler vv -12ww-34 xx -56yy -78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78);..../ / / /<= ruler vv 12ww34 xx +56yy 78zz 5/15/2015 ECE Application Programming: Lecture 4

13 printf() examples printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78);..../ / / /<= ruler v-0012w-34 x-0056y-0078z printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78);..../ / / /<= ruler v 0012w34 x+0056y00078z printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78);..../ / / /<= ruler v -012w -034x-056 y -078z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78);..../ / / /<= ruler v 012w 034x056 y +078z 5/15/2015 ECE Application Programming: Lecture 4

14 printf() examples printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6);..../ / / /<= ruler w -1.23x -3.46y -5.60z printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6);..../ / / /<= ruler w 1.23w 3.46y 5.60z printf("w%7.2fx%7.2f\n", , );..../ / / /<= ruler w x z 5/15/2015 ECE Application Programming: Lecture 4

Example: Formatted output Assume int x = 123; float y = 4.56; double z = What does each of the following lines print?  printf("%4d %5f %6lf\n", x, y, z);  printf("%.4d %.4f %.4lf\n", x, y, z);  printf("%08d %-7.1f %+-4.1lf !\n", x, y, z); The second and third specifiers have precision of 1 One is a float (%f), the other is a double (%lf) 5/15/2015 ECE Application Programming: Lecture 5 15

Example solution printf("%4d %5f %6lf\n", x, y, z);  █123█ █ printf("%.4d %.4f %.4lf\n", x, y, z);  0123█4.5600█ printf("%08d %-7.1f %+-4.1lf!\n", x, y, z);  █ 4.6 █████ +7.9 █ ! 5/15/2015 ECE Application Programming: Lecture 5 16

Example: Formatted output Write a short code sequence to do each of the following:  Print three integers—x, y, and z Use field widths of 10, 20, and 30, respectively Put an extra space between each field Show the signs of all values and left justify them  Print four double s—d1, d2, d3, d4 Use field widths of 7 for all values Put an extra space between each field Show 1, 2, 3, and 4 places after the decimal point, respectively  Given three variables— int w, p; double var; Read values for w and p from the input Print var using field width w and precision p 5/15/2015 ECE Application Programming: Lecture 5 17

Example solution Print three integers—x, y, and z  Use field widths of 10, 20, and 30, respectively  Put an extra space between each field  Show the signs of all values and left justify them printf(“%+-10d %+-20d %+-30d\n”, x, y, z); Print four double s—d1, d2, d3, d4  Use field widths of 7 for all values  Put an extra space between each field  Show 1, 2, 3, and 4 places after the decimal point, respectively printf(“%7.1lf %7.2lf %7.3lf %7.4lf\n”, d1, d2, d3, d4); 5/15/2015 ECE Application Programming: Lecture 5 18

Example solution (cont.) Given three variables— int w, p; double var;  Read values for w and p from the input  Print var using field width w and precision p scanf(“%d %d”, &w, &p); printf(“%*.*lf\n”, w, p, var); 5/15/2015 ECE Application Programming: Lecture 5 19

Next time Operators Conditional statements: if Reminders:  Program 2 due Wednesday, 2/6  Program 3 to be posted; due Wednesday, 2/13 5/15/2015 ECE Application Programming: Lecture 6 20