0% found this document useful (0 votes)
52 views35 pages

ch9 FormattedIO

This document discusses formatting output with printf in C. It covers printing integers, floating point numbers, strings, and characters using conversion specifiers. It also describes using field widths and precision to control the formatting of output. Precision for integers specifies the minimum number of characters printed, precision for floating point controls digits after the decimal, and precision for strings limits the maximum characters output.

Uploaded by

Praseeda Kalkur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views35 pages

ch9 FormattedIO

This document discusses formatting output with printf in C. It covers printing integers, floating point numbers, strings, and characters using conversion specifiers. It also describes using field widths and precision to control the formatting of output. Precision for integers specifies the minimum number of characters printed, precision for floating point controls digits after the decimal, and precision for strings limits the maximum characters output.

Uploaded by

Praseeda Kalkur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

1

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

Performs all input and output


Can often be redirected
- Standard input keyboard
- Standard output screen
- Standard error screen

2007 Pearson Education, Inc. All rights reserved.

9.3 Formatting Output with printf


printf
Precise output formatting
- Conversion specifications: flags, field widths, precisions, etc.

Can perform rounding, aligning columns, right/left justification,


inserting literal characters, exponential format, hexadecimal
format, and fixed width and precision

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

2007 Pearson Education, Inc. All rights reserved.

Fig. 9.1 | Integer conversion specifiers.

2007 Pearson Education, Inc. All rights reserved.

1
2

/* Fig 9.2: fig09_02.c */


/* Using the integer conversion specifiers */

3
4

#include <stdio.h>

5
6

int main( void )


{

7
8
9
10

printf( "%d\n", 455 );


printf( "%i\n", 455 );
printf( "%d\n", +455 );
printf( "%d\n", -455 );

Outline

d and i specify signed integers


fig09_02.c
/* i same as d in printf */

printf( "%hd\n", 32000 );


printf( "%ld\n", 2000000000L ); /* L suffix makes literal a long */

15
16

printf( "%u\n", -455 );

17
18

(1 of 2 )

h specifies a short number

11
12
13
14

printf( "%o\n", 455 );


printf( "%u\n", 455 );

l specifies a long number

o specifies an octal integer

printf( "%x\n", 455 );


printf( "%X\n", 455 );

u specifies an unsigned integer

19
return 0; /* indicates successful termination */
20
21 } /* end main */
455
455
455
-455
32000
2000000000
707
455
4294966841
1c7
1C7

x and X specify hexadecimal integers

2007 Pearson Education,


Inc. All rights reserved.

Common Programming Error 9.2


Printing a negative value with a conversion
specifier that expects an unsigned value.

2007 Pearson Education, Inc. All rights reserved.

9.5 Printing Floating-Point Numbers


Floating Point Numbers
Have a decimal point (33.5)
Exponential notation (computer's version of scientific
notation)
- 150.3 is 1.503 x 10 in scientific
- 150.3 is 1.503E+02 in exponential

2007 Pearson Education, Inc. All rights reserved.

Fig. 9.3 | Floating-point conversion specifiers.

2007 Pearson Education, Inc. All rights reserved.

Error-Prevention Tip 9.1


When outputting data, be sure that the user
is aware of situations in which data may be
imprecise due to formatting (e.g., rounding
errors from specifying precisions).

2007 Pearson Education, Inc. All rights reserved.

Outline

fig09_04.c

e and E specify exponential notation


f specifies fixed-point notation
g and G specify either exponential or fixed-point
notation depending on the numbers size

2007 Pearson Education,


Inc. All rights reserved.

10

9.6 Printing Strings and Characters


c
Prints char argument
Cannot be used to print the first character of a string

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')

2007 Pearson Education, Inc. All rights reserved.

11

Common Programming Error 9.3


Using %c to print a string is an error. The
conversion specifier %c expects a char
argument. A string is a pointer to char
(i.e., a char *).

2007 Pearson Education, Inc. All rights reserved.

/* Fig 9.5: fig09_05c */

/* Printing strings and characters */


#include <stdio.h>

3
4
5
6

Outline

int main( void )


{

fig09_05.c

char character = 'A'; /* initialize char */

8
9

char string[] = "This is a string"; /* initialize char array */

10
11

12

const char *stringPtr = "This is also a string"; /* char pointer */

12

printf( "%c\n", character );


printf( "%s\n", "This is a string" );

13
14

printf( "%s\n", string );


printf( "%s\n", stringPtr );

c specifies a character will be printed


s specifies a string will be printed

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

2007 Pearson Education,


Inc. All rights reserved.

9.8 Printing with Field Widths and


Precision

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

Integer width inserted between % and conversion specifier


%4d field width of 4

2007 Pearson Education, Inc. All rights reserved.

/* Fig 9.8: fig09_08.c */

/* Printing integers right-justified */

#include <stdio.h>

Outline

14

4
5

int main( void )

fig09_08.c

printf( "%4d\n", 1 );

printf( "%4d\n", 12 );

printf( "%4d\n", 123 );

10

printf( "%4d\n", 1234 );

11

printf( "%4d\n\n", 12345 );

A field width of 4 will make C attempt to


print the number in a 4-character space

(1 of 2 )

12
13

printf( "%4d\n", -1 );

14

printf( "%4d\n", -12 );

15

printf( "%4d\n", -123 );

16

printf( "%4d\n", -1234 );

17

printf( "%4d\n", -12345 );

Note that C considers the minus sign a character

18
19

return 0; /* indicates successful termination */

20
21 } /* end main */
1
12
123
1234
12345
-1
-12
-123
-1234
-12345

2007 Pearson Education,


Inc. All rights reserved.

15

Common Programming Error 9.8


Not providing a sufficiently large field
width to handle a value to be printed can
offset other data being printed and can
produce confusing outputs. Know your data!

2007 Pearson Education, Inc. All rights reserved.

9.8 Printing with Field Widths and


Precision

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

2007 Pearson Education, Inc. All rights reserved.

/* Fig 9.9: fig09_09.c */

/* Using precision while printing integers,

3
4

Outline

floating-point numbers, and strings */


#include <stdio.h>

Precision for integers specifies the minimum


number of characters to be printed

5
6

int main( void )

17

fig09_09.c

(1 of 2 )

int i = 873;

/* initialize int i */

double f = 123.94536;

/* initialize double f */

10

char s[] = "Happy Birthday"; /* initialize char array s */

11
12

printf( "Using precision for integers\n" );

13

printf( "\t%.4d\n\t%.9d\n\n", i, i );

Precision for the g specifier controls the


maximum number of significant digits printed

14
15

printf( "Using precision for floating-point numbers\n" );

16
17

printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );

18

printf( "Using precision for strings\n" );

19

printf( "\t%.11s\n", s );

Precision for f and e specifiers controls the


number of digits after the decimal point

20
21

return 0; /* indicates successful termination */

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

Precision for strings specifies the maximum


number of characters to be printed

2007 Pearson Education,


Inc. All rights reserved.

/* Fig 9.9: fig09_09.c */

/* Using precision while printing integers,

3
4

Outline

floating-point numbers, and strings */


#include <stdio.h>

Precision for integers specifies the minimum


number of characters to be printed

5
6

int main( void )

18

fig09_09.c

(1 of 2 )

int i = 873;

/* initialize int i */

double f = 123.94536;

/* initialize double f */

10

char s[] = "Happy Birthday"; /* initialize char array s */

11
12

printf( "Using precision for integers\n" );

13

printf( "\t%.4d\n\t%.9d\n\n", i, i );

Precision for the g specifier controls the


maximum number of significant digits printed

14
15

printf( "Using precision for floating-point numbers\n" );

16
17

printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );

18

printf( "Using precision for strings\n" );

19

printf( "\t%.11s\n", s );

Precision for f and e specifiers controls the


number of digits after the decimal point

20
21

return 0; /* indicates successful termination */

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

Precision for strings specifies the maximum


number of characters to be printed

2007 Pearson Education,


Inc. All rights reserved.

/* Fig 9.9: fig09_09.c */

/* Using precision while printing integers,

3
4

Outline

floating-point numbers, and strings */


#include <stdio.h>

Precision for integers specifies the minimum


number of characters to be printed

5
6

int main( void )

19

fig09_09.c

(1 of 2 )

int i = 873;

/* initialize int i */

double f = 123.94536;

/* initialize double f */

10

char s[] = "Happy Birthday"; /* initialize char array s */

11
12

printf( "Using precision for integers\n" );

13

printf( "\t%.4d\n\t%.9d\n\n", i, i );

Precision for the g specifier controls the


maximum number of significant digits printed

14
15

printf( "Using precision for floating-point numbers\n" );

16
17

printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );

18

printf( "Using precision for strings\n" );

19

printf( "\t%.11s\n", s );

Precision for f and e specifiers controls the


number of digits after the decimal point

20
21

return 0; /* indicates successful termination */

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

Precision for strings specifies the maximum


number of characters to be printed

2007 Pearson Education,


Inc. All rights reserved.

9.9 Using Flags in the printf Format


Control String

20

Flags
Supplement formatting capabilities
Place flag immediately to the right of percent sign
Several flags may be combined

2007 Pearson Education, Inc. All rights reserved.

21

Fig. 9.10 | Format control string flags.

2007 Pearson Education, Inc. All rights reserved.

/* Fig 9.11: fig09_11.c */

/* Right justifying and left justifying values */

#include <stdio.h>

Outline

22

4
5

int main( void )

fig09_11.c

printf( "%10s%10d%10c%10f\n\n", "hello", 7, 'a', 1.23 );

printf( "%-10s%-10d%-10c%-10f\n", "hello", 7, 'a', 1.23 );

9
10

return 0; /* indicates successful termination */

11
12 } /* end main */
hello
hello

7
7

- flag left justifies characters in a field

1.230000
1.230000

2007 Pearson Education,


Inc. All rights reserved.

Outline

23

fig09_12.c

+ flag forces a plus sign on positive numbers

2007 Pearson Education,


Inc. All rights reserved.

/* Fig 9.13: fig09_13.c */

/* Printing a space before signed values

3
4

Outline

not preceded by + or - */

24

#include <stdio.h>

5
6

int main( void )

printf( "% d\n% d\n", 547, -547 );

9
10

fig09_13.c

Space flag forces a space on positive numbers

return 0; /* indicates successful termination */

11
12 } /* end main */
547
-547

2007 Pearson Education,


Inc. All rights reserved.

/* Fig 9.14: fig09_14.c */

2
3
4

/* Using the # flag with conversion specifiers


o, x, X and any floating-point specifier */
#include <stdio.h>

Outline

5
6

int main( void )

fig09_14.c

7
8

{
int c = 1427;

/* initialize c */

9
10

double p = 1427.0; /* initialize p */

11
12

printf( "%#o\n", c );

13
14
15

25

printf( "%#x\n", c );
printf( "%#X\n", c );
printf( "\n%g\n", p );

# flag prefixes a 0 before octal integers


# flag prefixes a 0x before hexadecimal integers

printf( "%#g\n", p );

16
17
return 0; /* indicates successful termination */
18
19 } /* end main */

# flag forces a decimal point on floatingpoint numbers with no fractional part

02623
0x593
0X593
1427
1427.00

2007 Pearson Education,


Inc. All rights reserved.

/* Fig 9.15: fig09_15.c */

/* Printing with the 0( zero ) flag fills in leading zeros */

#include <stdio.h>

Outline

26

4
5

int main( void )

fig09_15.c

printf( "%+09d\n", 452 );

printf( "%09d\n", 452 );

0 flag fills empty spaces with zeros

9
10

return 0; /* indicates successful termination */

11
12 } /* end main */
+00000452
000000452

2007 Pearson Education,


Inc. All rights reserved.

9.10 Printing Literals and Escape


Sequences

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

2007 Pearson Education, Inc. All rights reserved.

28

Fig. 9.16 | Escape sequences.

2007 Pearson Education, Inc. All rights reserved.

29

9.11 Formatting Input with scanf


scanf
Input can be formatted much like output can
scanf conversion specifiers are slightly different from
those used with printf

2007 Pearson Education, Inc. All rights reserved.

30

Fig. 9.17 | Conversion specifiers for scanf. (Part 1 of 3.)

2007 Pearson Education, Inc. All rights reserved.

31

Fig. 9.17 | Conversion specifiers for scanf. (Part 2 of 3.)

2007 Pearson Education, Inc. All rights reserved.

/* Fig 9.18: fig09_18.c */

/* Reading integers */
#include <stdio.h>

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

int main( void )


{
int a;
int b;
int c;
int d;
int e;
int f;
int g;

Outline

d specifies a decimal integer will be input

32

fig09_18.c

i specifies an integer will be input


o specifies an octal integer will be input
u specifies an unsigned decimal integer will be input
x specifies a hexadecimal integer will be input

printf( "Enter seven integers: " );


scanf( "%d%i%i%i%o%u%x", &a, &b, &c, &d, &e, &f, &g );
printf( "The input displayed as decimal integers is:\n" );
printf( "%d %d %d %d %d %d %d\n", a, b, c, d, e, f, g );
return 0; /* indicates successful termination */

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

2007 Pearson Education,


Inc. All rights reserved.

Outline

e, f, and g specify a floating-point number will be input

33

fig09_19.c

l specifies a double or long double will be input

2007 Pearson Education,


Inc. All rights reserved.

/* Fig 9.20: fig09_20.c */

/* Reading characters and strings */

#include <stdio.h>

Outline

34

4
5

int main( void )

char x;

char y[ 9 ];

c specifies a character will be input

fig09_20.c

s specifies a string will be input

10

printf( "Enter a string: " );

11

scanf( "%c%s", &x, y );

12
13

printf( "The input was:\n" );

14

printf( "the character \"%c\" ", x );

15

printf( "and the string \"%s\"\n", y );

16
17

return 0; /* indicates successful termination */

18
19 } /* end main */
Enter a string: Sunday
The input was:
the character "S" and the string "unday"

2007 Pearson Education,


Inc. All rights reserved.

Outline

35

fig09_24.c

* is a wildcardscanf will disregard anything


between the two inputs on either side of it

2007 Pearson Education,


Inc. All rights reserved.

You might also like