chapter03 - COP 3275: Chapter 03 Jonathan C.L. Liu, Ph.D....

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

View Full Document Right Arrow Icon
COP 3275: Chapter 03 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Background image of page 1

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

View Full DocumentRight Arrow Icon
The printf Function The printf function must be supplied with a format string, followed by any values that are to be inserted into the string during printing: printf( string , expr 1 , expr 2 , …); The format string may contain both ordinary characters and conversion specifications, which begin with the % character. A conversion specification is a placeholder representing a value to be filled in during printing. %d is used for int values %f is used for float values 2
Background image of page 2
The printf Function Ordinary characters in a format string are printed as they appear in the string; conversion specifications are replaced. Example: int i, j; float x, y; i = 10; j = 20; x = 43.2892f; y = 5527.0f; printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y); Output: i = 10, j = 20, x = 43.289200, y = 5527.000000 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
The printf Function Compilers aren’t required to check that the number of conversion specifications in a format string matches the number of output items. Too many conversion specifications: printf("%d %d\n", i); /*** WRONG ***/ Too few conversion specifications: printf("%d\n", i, j); /*** WRONG ***/ 4
Background image of page 4
The printf Function Compilers aren’t required to check that a conversion specification is appropriate. If the programmer uses an incorrect specification, the program will produce meaningless output: printf("%f %d\n", i, x); /*** WRONG ***/ 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Conversion Specifications A conversion specification can have the form % m . pX or %- m . pX , where m and p are integer constants and X is a letter. Both m and p are optional; if p is omitted, the period that separates m and p is also dropped. In the conversion specification %10.2f , m is 10, p is 2, and X is f . In the specification %10f , m is 10 and p (along with the period) is missing, but in the specification %.2f , p is 2 and m is missing. 6
Background image of page 6
The minimum field width, m , specifies the minimum number of characters to print. If the value to be printed requires fewer than m characters, it is right-justified within the field. %4d displays the number 123 as •123 . (• represents the space character.) If the value to be printed requires more than m characters, the field width automatically expands to the necessary size. Putting a minus sign in front of
Background image of page 7

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

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

This note was uploaded on 11/30/2011 for the course COP 3275 taught by Professor Jonathanliu during the Fall '11 term at University of Florida.

Page1 / 29

chapter03 - COP 3275: Chapter 03 Jonathan C.L. Liu, Ph.D....

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

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