lecture05-formatted IO

Space nonnegative numbers produced by signed

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

View Full Document Right Arrow Icon
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 first 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 first)   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

Ask a homework question - tutors are online