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

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

Embed Size (px)

Citation preview

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

16.216ECE Application

ProgrammingInstructor: Dr. Michael Geiger

Spring 2013

Lecture 6:printf() formatting

Page 2: 16.216 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

04/18/23ECE Application Programming:

Lecture 6 2

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

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); 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

04/18/23ECE Application Programming:

Lecture 4 3

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

Formatted output: precision For examples, int n = 12; double x = 3.754 Specifying precision:

# 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

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

04/18/23ECE Application Programming:

Lecture 4 4

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

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!

04/18/23ECE Application Programming:

Lecture 4 5

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

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

04/18/23ECE Application Programming:

Lecture 4 6

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

7

printf format specificationsGeneral form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type

flag meaning default

- Left align the result within the given field width Right 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 (-)

0 If 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

04/18/23ECE Application Programming:

Lecture 4

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

8

printf format specificationsGeneral 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 Library04/18/23ECE Application Programming:

Lecture 4

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

9

printf format specificationsGeneral form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type

typ meaning default

c The precision has no effect character 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,E The 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

f The 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,S The 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 Library04/18/23ECE Application Programming:

Lecture 4

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

10

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.

04/18/23ECE Application Programming:

Lecture 4

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

11

examples - printf()float a=67.49,b=9.999925;int i=184,j=-51;double x=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); //changedPrinted:00000000011111111112222222222333333 print12345678901234567890123456789012345 position

-22.3 184 67.49 columns 10.00 -51 -22.33 1 to 35Notes: d indicates decimal integer

f indicates floatlf (that’s lower case L and f) indicates double(for double output, f OK too; not OK for input)

04/18/23ECE Application Programming:

Lecture 4

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

12

printf() examplesprintf("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

04/18/23ECE Application Programming:

Lecture 4

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

13

printf() examplesprintf("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

04/18/23ECE Application Programming:

Lecture 4

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

14

printf() examplesprintf("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

04/18/23ECE Application Programming:

Lecture 4

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

Example: 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)

04/18/23ECE Application Programming:

Lecture 5 15

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

Example 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█!

04/18/23ECE Application Programming:

Lecture 5 16

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

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 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

04/18/23ECE Application Programming:

Lecture 5 17

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

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 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);

04/18/23ECE Application Programming:

Lecture 5 18

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

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);

04/18/23ECE Application Programming:

Lecture 5 19

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

Next time Operators Conditional statements: if

Reminders: Program 2 due Wednesday, 2/6 Program 3 to be posted; due Wednesday, 2/13

04/18/23ECE Application Programming:

Lecture 6 20