C Programming Tutorial (K&R version 4) _Part2

C Programming Tutorial (K&R version 4) _Part2 -...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Formatting with printf The example program above does not produce a very neat layout on the screen. The conversion specifiers in the printf string can be extended to give more information. The % and the character type act like brackets around the extra information. e.g. %-10.3f is an extended version of %f , which carries some more information. That extra information takes the form: % [-] [ fwidth ] [. p ] X where the each bracket is used to denote that the item is optional and the symbols inside them stand for the following. [ fwidth ] This is a number which specifies the field width of this "blank field". In other words, how wide a space will be made in the string for the object concerned? In fact it is the minimum field width because if data need more room than is written here they will spill out of their box of fixed size. If the size is bigger than the object to be printed, the rest of the field will be filled out with spaces. [-] If this included the output will be left justified. This means it will be aligned with the left hand margin of the field created with [ fwidth ] . Normally all numbers are right justified, or aligned with the right hand margin of the field "box". [. p ] This has different meanings depending on the object which is to be printed. For a floating point type ( float or double ) p specifies the number of decimal places after the point which are to be printed. For a string it specifies how many characters are to be printed. Some valid format specifiers are written below here. %10d %2.2f %25.21s %2.6f The table below helps to show the effect of changing these format controls. The width of a field is draw in by using the | bars. Object to Control Spec. Actual Output be printed 42 %6d | 42| 42 %-6d |42 | 324 %10d | 324| -1 %-10d |-1 | -1 %1d |-1|(overspill) 'z' %3c | z| 'z' %-3c |z | 2.71828 %10f | 2.71828| 2.71828 %10.2f | 2.71| 2.71828 %-10.2f |2.71 | 2.71828 %2.4f |2.7182|(overspill) 2.718 %.4f |2.7180| 2.718 %10.5f | 2.71800| 2.71828 %10e |2.71828e+00| 2.71828 %10.2e | 2.17e+00| 2.71828 %10.2g | 2.71| "printf" %s |printf| "printf" %10s | printf| "printf" %2s |printf|(overspill) "printf" %5.3s | pri| "printf" %-5.3s |pri | "printf" %.3s |pri| Node:Example 10, Next: Output 10 , Previous: Formatting with printf , Up: Standard Output and Standard Input Example Listing /***********************************************/ /* */ /* Multiplication Table */ /* */ /***********************************************/ #include <stdio.h> main () /* Printing in columns */ { int i,j; for (i = 1; i <= 10; i++) { for (j = 1; j <= 10; j++) { printf ("%5d",i * j); } printf ("\n"); } } Node:Output 10, Next: Special Control Characters again , Previous: Example 10 , Up: Standard Output and Standard Input Output 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72...
View Full Document

This document was uploaded on 02/08/2012.

Page1 / 89

C Programming Tutorial (K&R version 4) _Part2 -...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online