ch9 FormattedIO
ch9 FormattedIO
Streams
Streams
Sequences of characters organized into lines
- Each line consists of zero or more characters and ends with
newline character
- ANSI C must support lines of at least 254 characters
Format
printf( format-control-string, other-arguments );
Conversion specification:
Flag
Min_Width
Precision
Size Code
Format control string: describes output format
Other-arguments: correspond to each conversion specification in
format-control-string
- Each specification begins with a percent sign(%), ends with
conversion specifier
1
2
3
4
#include <stdio.h>
5
6
7
8
9
10
Outline
15
16
17
18
(1 of 2 )
11
12
13
14
19
return 0; /* indicates successful termination */
20
21 } /* end main */
455
455
455
-455
32000
2000000000
707
455
4294966841
1c7
1C7
Outline
fig09_04.c
10
s
Requires a pointer to char as an argument
Prints characters until NULL ('\0') encountered
Cannot print a char argument
Remember
Single quotes for character constants ('z')
Double quotes for strings "z" (which actually contains two
characters, 'z' and '\0')
11
3
4
5
6
Outline
fig09_05.c
8
9
10
11
12
12
13
14
15
16
return 0; /* indicates successful termination */
17
18 } /* end main */
A
This is a string
This is a string
This is also a string
13
Field width
Size of field in which data is printed
If width larger than data, default right justified
- If field width too small, increases to fit data
- Minus sign uses one character position in field
#include <stdio.h>
Outline
14
4
5
fig09_08.c
printf( "%4d\n", 1 );
printf( "%4d\n", 12 );
10
11
(1 of 2 )
12
13
printf( "%4d\n", -1 );
14
15
16
17
18
19
20
21 } /* end main */
1
12
123
1234
12345
-1
-12
-123
-1234
-12345
15
16
Precision
Meaning varies depending on data type
Integers (default 1)
- Minimum number of digits to print
If data too small, prefixed with zeros
Floating point
- Number of digits to appear after decimal (e and f)
For g maximum number of significant digits
Strings
- Maximum number of characters to be written from string
Format
- Use a dot (.) then precision number after %
%.3f
3
4
Outline
5
6
17
fig09_09.c
(1 of 2 )
int i = 873;
/* initialize int i */
double f = 123.94536;
/* initialize double f */
10
11
12
13
printf( "\t%.4d\n\t%.9d\n\n", i, i );
14
15
16
17
printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );
18
19
printf( "\t%.11s\n", s );
20
21
22
23 } /* end main */
Using precision for integers
0873
000000873
Using precision for floating-point numbers
123.945
1.239e+002
124
Using precision for strings
3
4
Outline
5
6
18
fig09_09.c
(1 of 2 )
int i = 873;
/* initialize int i */
double f = 123.94536;
/* initialize double f */
10
11
12
13
printf( "\t%.4d\n\t%.9d\n\n", i, i );
14
15
16
17
printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );
18
19
printf( "\t%.11s\n", s );
20
21
22
23 } /* end main */
Using precision for integers
0873
000000873
Using precision for floating-point numbers
123.945
1.239e+002
124
Using precision for strings
3
4
Outline
5
6
19
fig09_09.c
(1 of 2 )
int i = 873;
/* initialize int i */
double f = 123.94536;
/* initialize double f */
10
11
12
13
printf( "\t%.4d\n\t%.9d\n\n", i, i );
14
15
16
17
printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );
18
19
printf( "\t%.11s\n", s );
20
21
22
23 } /* end main */
Using precision for integers
0873
000000873
Using precision for floating-point numbers
123.945
1.239e+002
124
Using precision for strings
20
Flags
Supplement formatting capabilities
Place flag immediately to the right of percent sign
Several flags may be combined
21
#include <stdio.h>
Outline
22
4
5
fig09_11.c
9
10
11
12 } /* end main */
hello
hello
7
7
1.230000
1.230000
Outline
23
fig09_12.c
3
4
Outline
not preceded by + or - */
24
#include <stdio.h>
5
6
9
10
fig09_13.c
11
12 } /* end main */
547
-547
2
3
4
Outline
5
6
fig09_14.c
7
8
{
int c = 1427;
/* initialize c */
9
10
11
12
printf( "%#o\n", c );
13
14
15
25
printf( "%#x\n", c );
printf( "%#X\n", c );
printf( "\n%g\n", p );
printf( "%#g\n", p );
16
17
return 0; /* indicates successful termination */
18
19 } /* end main */
02623
0x593
0X593
1427
1427.00
#include <stdio.h>
Outline
26
4
5
fig09_15.c
9
10
11
12 } /* end main */
+00000452
000000452
27
Printing Literals
Most characters can be printed
Certain "problem" characters, such as the quotation mark
"
Must be represented by escape sequences
- Represented by a backslash \ followed by an escape
character
28
29
30
31
/* Reading integers */
#include <stdio.h>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Outline
32
fig09_18.c
22
23 } /* end main */
Enter seven integers: -70 -70 070 0x70 70 70 70
The input displayed as decimal integers is:
-70 -70 56 112 56 70 112
Outline
33
fig09_19.c
#include <stdio.h>
Outline
34
4
5
char x;
char y[ 9 ];
fig09_20.c
10
11
12
13
14
15
16
17
18
19 } /* end main */
Enter a string: Sunday
The input was:
the character "S" and the string "unday"
Outline
35
fig09_24.c