1 Lab guide 8 C Formatted Input Output

1 Lab guide #8 C Formatted Input / Output Review and Lab Assignments 2007 Pearson Education, Inc. All rights reserved.

2 Key points § Input and output streams. § Format output using printf § Format input using scanf. 2007 Pearson Education, Inc. All rights reserved.

3 Input and output streams. § Streams – Sequences of characters organized into lines – Performs all input and output – Can often be redirected § Input: – printf, sprint, puts, putchar § Output: – scanf, sscanf, gets, getchar 2007 Pearson Education, Inc. All rights reserved.

4 Format output using printf § printf( format-control-string, other-arguments ); – printf( "t%. 3 fnt%. 3 ent%. 3 gnn", f, f, f ); § Conversion specifiers – – Integer: d, i, o, u, x, X, h, l Floating point: f, e, E, g, G, L Character and String: c, s Others: p, n, % § Print with field widths and precision – Field widths: Size of field in which data is printed - Syntax: printf( "%4 dn", -12 ); – Precision: Integer, Floating point, String - Syntax: printf( "t%. 4 dnt%. 9 dnn", i, i ); § Flags: -, +, space, #, 0 § Escape sequence: 2007 Pearson Education, Inc. All rights reserved.

5 Format input using scanf § Scanf(format-control-string, other-arguments) – scanf( "%d%i%i%i%o", &a, &b, &c, &d, &e, ); § Format control string: – Contain conversion specifiers only – Conversion specifiers: - Integers, Floating point, Character and String - Scan set [scan characters] 2007 Pearson Education, Inc. All rights reserved.

6 Lab Assignments 1. fig 09_14. c (Lecture note) 2. fig 09_24. c (Lecture note) 3. Write a program to convert a temperature in degrees Fahrenheit to degrees Celsius. – Problem input: fahrenheit : integer (temperature in degrees Fahrenheit) – Problem output: celsius : real (temperature in degrees Celsius) – Relevant formula celsius = (5. 0/9. 0) x (fahrenheit- 32. 0) 2007 Pearson Education, Inc. All rights reserved.

4. In some programming languages, strings are entered surrounded by either single or double quotation marks. Write a program that reads the three strings suzy, “suzy”and ‘suzy’. Are the single or double quotes ignored by C or read as part of the string? 2007 Pearson Education, Inc. All rights reserved.
- Slides: 7