Ch. 8: Formatted Input and Output

Ch 8 Formatted - Formatted Input/Output Outline Formatting Output with printf Printing Integers Escape Characters Printing Floating-Point Numbers

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

View Full Document Right Arrow Icon
Formatted Input/Output Outline Formatting Output with printf Printing Integers Escape Characters Printing Floating-Point Numbers Printing Strings and pointers Field Widths and Precisions Flags Formatting Input with scanf Sample Problem Function getnum() Extensions in Ch Default Formatted Input and Output Formatted Block of Output Using fprintf
Background image of page 1

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

View Full DocumentRight Arrow Icon
Formatting Output with printf Precisely formatted output is accomplished using the output function printf . The printf function has following form printf( format-control-string, arguments ); Format-control-string: Using specifications to describe output format. Each specification begins with a percent sign (%), ends with conversion specifier and is enclosed in quotation marks. – arguments: correspond to each conversion specification in format-
Background image of page 2
Example: > int i = 5 > float f = 1.234 > double d = 123.4567 > printf(“i = %d\n”, i) i = 5 > printf(“f = %f\n”, f) f = 1.234000 > printf(“d = %lf\n”, d) d = 123.456700 > printf(“i = %d, f = %f, d = %lf\n”, i, f, d) i = 5, f = 1.234000, d = 123.456700 • Conversion specifications: flags, field widths, precisions, etc. • The function printf can perform rounding, aligning columns, right/left justification, inserting literal characters, fixed width, etc.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Printing Integers Table below lists the integer conversion specifiers and gives short description for each conversion specifier. Table Conversion specifiers for integer Conversion Specifier Description c Display a character. d Display a signed decimal integer. i Display a signed decimal integer. b Display an unsigned binary integer. o Display an unsigned octal integer. x or X Display an unsigned hexadecimal integer. h or l or ll Place before any integer conversion identifier to indicate short , long , or long long integer. u Display an unsigned decimal integer. Such as %u, %hu, %lu, %llu.
Background image of page 4
Example1: /* File: intio.c */ #include <stdio.h> int main() { short i1 = 0x20; long i2 = 0x20000000; printf("%c\n", 98); printf("%d\n", 98); printf("%d\n", -98); printf("%i\n", 98); printf(“0%o\n", 98); printf(“0x%x\n", 98); printf(“0X%X\n", 98); printf("%hd\n", i1); printf("%ld\n", i2); printf("%d\n", i2); return 0; } Output: b 98 -98 98 0142 0x62 0X62 32 536870912 536870912
Background image of page 5

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

View Full DocumentRight Arrow Icon
Example2: > int i = 98 > printf(“i = 0b%b\n”, i) i = 0b1100010 > long long l > l = 0x200000 2097152 > printf(“l = %lld”, l) l = 2097152
Background image of page 6
Escape Characters Escape Code Description \a (alert) Produces an audible or visible alert. The active position shall not be changed. \b (backspace) Moves the active position to the previous position on the current line. \f (form feed) Moves the active position to the initial position at the start of the next logical page. \n
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 04/21/2008 for the course EME 005 taught by Professor Cheng during the Fall '07 term at UC Davis.

Page1 / 29

Ch 8 Formatted - Formatted Input/Output Outline Formatting Output with printf Printing Integers Escape Characters Printing Floating-Point Numbers

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