1 / 21

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2011 Lecture 8: Output formatting. Lecture outline. Announcements/reminders Assignment 3 posted, due 9/26 Submit through Blackboard: https://continuinged.uml.edu/login/login.cfm

ronia
Download Presentation

16.216 ECE Application Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 8: Output formatting

  2. Lecture outline • Announcements/reminders • Assignment 3 posted, due 9/26 • Submit through Blackboard: https://continuinged.uml.edu/login/login.cfm • Assignment 2 grading done soon (hopefully today) • Review • Bitwise operators • Today • Formatted output ECE Application Programming: Lecture 8

  3. Review: bit manipulation • Bitwise operators: | & ^ ~ • Used for desired logical operations • Used to set/clear bits • Bit shifts: << >> • Used to shift bits into position • Used for multiplication by powers of 2 • Common operations • Setting/clearing individual bit • Masking out ECE Application Programming: Lecture 9

  4. 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 0flag before width • printf(“%010d”, n);  0000000012 • printf(“%+010d”, n);  +000000012 • 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 ECE Application Programming: Lecture 8

  5. Formatted output: precision • For examples, int n = 12; double x = 3.754 • Specifying precision: • Minimum # chars for integer (%d, %i, %u, %x) • Does not truncate; will pad with leading 0s • printf(“%.1d”, n);  12 • printf(“%.3d”, n);  012 • # chars after decimal point for FP (%f, %lf) • Rounds last digit • printf(“%.6lf”, x);  3.754000 • printf(“%.1lf”, x);  3.8 • printf(“%.0lf”, x);  4 • 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 ECE Application Programming: Lecture 8

  6. Formatted output: hexadecimal • To print a number in hex, use %x or %X • %x prints characters a-f in lowercase • %X prints characters A-F in uppercase • To show leading 0x, use the # flag • To show leading 0s, use desired field width with 0 flag • Examples (assume var1 = 0x1A2B) • printf(“%x”, var1)  1a2b • printf(“%X”, var1)  1A2B • printf(“%#x”, var1)  0x1a2b • printf(“%06x”, var1)  001a2b • printf(“%#06x”, var1)  0x1a2b ECE Application Programming: Lecture 8

  7. 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 ECE Application Programming: Lecture 8

  8. printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type This slide adapted from information in MSDN Library ECE Application Programming: Lecture 8

  9. 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). ECE Application Programming: Lecture 8 This slide adapted from information in MSDN Library

  10. printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type ECE Application Programming: Lecture 8 This slide adapted from information in MSDN Library

  11. examples - printf() float a=67.49,b=9.999925;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: 00000000011111111112222222222333333 print12345678901234567890123456789012345 positionPrice:██67.499000███9.999925 columns███67.49██67.4900██10.0█9.99993 1 to 35a=67.49000,█b=10.00 Note: On last line of output, the actual value was output; the printf routine overrode the specified width. ECE Application Programming: Lecture 8

  12. examples - printf() float a=67.49,b=9.999925;inti=184,j=-51;doublex=123.456, 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: 00000000011111111112222222222333333 print12345678901234567890123456789012345 position -22.3 184 67.49 columns 10.00 -51 -22.33 1 to 35 Notes: d indicates decimal integerf indicates floatlf (that’s lower case L and f) indicates double(for double output, f OK too; not OK for input) ECE Application Programming: Lecture 8

  13. printf() examples printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulervv-12ww-34xx-56yy-78zz printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulervv 12ww34xx+56yy78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulervv -12ww-34 xx -56yy -78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulervv 12ww34 xx +56yy 78zz ECE Application Programming: Lecture 8

  14. printf() examples printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulerv-0012w-34 x-0056y-0078z printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulerv 0012w34 x+0056y00078z printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulerv -012w -034x-056 y -078z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78); ..../....1..../....2..../....3..../ <= rulerv 012w 034x056 y +078z ECE Application Programming: Lecture 8

  15. printf() examples printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6); ..../....1..../....2..../....3..../ <= rulerw -1.23x -3.46y -5.60z printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6); ..../....1..../....2..../....3..../ <= rulerw 1.23w 3.46y 5.60z printf("w%7.2fx%7.2f\n",-1234567.8,1234567.8); ..../....1..../....2..../....3..../ <= rulerw-1234567.8x1234567.8z ECE Application Programming: Lecture 8

  16. Example 1: Formatted output • Assume int x = 123; float y = 4.56; double z = 7.89991 • 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) ECE Application Programming: Lecture 8

  17. Example 1 solution • printf("%4d %5f %6lf\n", x, y, z);  █123█4.560000█7.899910 • printf("%.4d %.4f %.4lf\n", x, y, z);  0123█4.5600█7.8999 • printf("%08d %-7.1f %+-4.1lf!\n", x, y, z); 00000123█4.6█████+7.9█! ECE Application Programming: Lecture 8

  18. Example 2: 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 doubles—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 ECE Application Programming: Lecture 8

  19. Example 2 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 doubles—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); ECE Application Programming: Lecture 8

  20. Example 2 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); ECE Application Programming: Lecture 8

  21. Next time • Programming exercise 1 • Formatting • Debugging ECE Application Programming: Lecture 8

More Related