lecture05-formatted IO

# Space nonnegative numbers produced by signed

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: es left justification         default is right justification there are other flags possible here as well m (optional) specifies the minimum field width. .p (optional) specifies:         Number of digits after the decimal point (for a floating point number) or Minimum number of digits to print (for an integer) X is the basic data format used (e.g. d,e,f) For a more complete description of conversion specifications see Chapter 22.3 10 Why use printf and scanf? 11 …printf Conversion Specifications   Flags (optional; more than one permitted): Flag + Meaning Left-justify within field. Numbers produced by signed conversions always begin with + or -. Space Nonnegative numbers produced by signed conversion are preceded by a space. # Octal numbers begin with 0, nonzero hexadecimal numbers with 0x or 0X. Floating-point numbers always have a decimal point. Trailing zeros aren t removed from numbers printed with the g or G conversion codes. 0 Numbers are padded with leading zeros up to the field width 12 What do they print?   prinA( %d , 4);   4   prinA( %2d , 4);   4 (one space ﬁrst before 4)   prinA( % ­2d , 4);   4  (one space last)   prinA( %2.2f , 4.4);   4.40 (no spaces)   prinA( %4.2f , 4.4)   4.40 (no spaces)   prinA( %5.2f , 4.4);   4.40 (one space ﬁrst)   prinA( %.0f , 4.4);   4 13 Example #include <stdio.h> int main(void) { /* example of forma&ed output */ int i; double f; i = 40; f = 839.21; Escape Sequence \a Alert (bell) \b Backspace \n New Line \t Horizontal Tab \” “ \\ \ prin%("|%d|%5d|% ­5d|%5.3d|\n", i, i, i, i); prinA("|%10.3f|%10.3e|% ­10g|\n", f, f, f); return 0; } 6. Output of 1st prin% appears as ??? (mark a * to indicate a space): NOTE: the “e+02” count as 4 characters. 14 The scanf Function “the pattern matching function...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern