Basic I/O - Printf
Basic I/O - Printf
CSCI 230
IUPUI
Dale Roberts
Formatted Input/Output
In this chapter
Presentation of results
scanf and printf
Streams (input and output)
gets, puts, getchar, putchar (in <stdio.h>)
Streams
Dale Roberts
(cont.)
Format
printf( format-control-string, otherarguments);
Format control string: describes output format,
Ordinary characters: copy to output stream: printf(this is
an output\n);
Conversion specifications: leading with character %
Format:
%-w.plx
(cont.)
Dale Roberts
Dale Roberts
Printing Integers
Whole number (no decimal point): 25, 0, -9
Positive, negative, or zero
Only minus sign prints by default
Example:
Program
Output
455
455
455
-455
32000
2000000000
707
455
65081
1c7
1C7
Dale Roberts
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 }
Program Output
return 0;
Dale Roberts
%s
Example:
1 /* Fig 9.5: fig09_05c */
2 /* Printing strings and characters */
3 #include <stdio.h>
4
5 int main()
6 {
7
char character = 'A';
8
char string[] = "This is a string";
9
const char *stringPtr = "This is also a
string";
10
11
printf( "%c\n", character );
12
printf( "%s\n", "This is a string" );
13
printf( "%s\n", string );
14
printf( "%s\n", stringPtr );
15
16
return 0;
17 }
Program Output
A
This is a string
This is a string
This is also a
string
Dale Roberts
%n
Stores number of characters already output by current printf statement
Takes a pointer to an integer as an argument
Nothing printed by a %n specification
Every printf call returns a value
Number of characters output
Negative number if error occurs
%%
Prints a percent sign
Dale Roberts
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&y
16
17
18
19
20
21
22
23
int main()
{
int *ptr;
int x = 12345, y;
Dale Roberts
Example:
%.3f
Dale Roberts
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
Program Output:
/* Fig 9.9: fig09_09.c */
/* Using precision while printing integers,Using precision for integers
0873
floating-point numbers, and strings */
000000873
#include <stdio.h>
int main()
{
int i = 873;/* Initialize variables */
double f = 123.94536;
char s[] = "Happy Birthday";
/* print */
14
15
16
17
18
19
20 }
printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );
printf( "Using precision for strings\n" );
printf( "\t%.11s\n", s );
return 0;
Dale Roberts
Description
- (minus sign)
+ (plus sign)
Display a plus sign preceding positive values and a minus sign preceding negative
values.
space
0 (zero)
Print a space before a positive value not printed with the + flag
Prefix 0 to the output value when used with the octal conversion specifier
Prefix 0x or 0X to the output value when used with the hexadecimal conversion
specifiers x or X
Force a decimal point for a floating point number printed with e, E, f, g, or G
that does not contain a fractional part. (Normally the decimal point is only printed
if a digit follows it.) For g and G specifiers, trailing zeros are not eliminated.
Pad a field with leading zeros
Dale Roberts
Example:
1 /* Fig 9.11: fig09_11.c */
2 /* Right justifying and left justifying values */
3 #include <stdio.h>
4
5 int main()
6 {
7
printf( "%10s%10d%10c%10f\n\n", "hello", 7, 'a',
1.23
);
8
printf( "%-10s%-10d%-10c%-10f\n", "hello", 7,
'a',
1.23
); 0;
9
return
10 }
Program Output:
hello7a1.230000
hello7a1.230000
Dale Roberts
Example:
1 /* Fig 9.14: fig09_14.c */
2 /* Using the # flag with conversion
specifiers
3
o, x, X and any floating-point
specifier
4 #include*/<stdio.h>
5
6 int main()
7 {
8
int c = 1427;
9
double p = 1427.0;
10
11
printf( "%#o\n", c );
12
printf( "%#x\n", c );
13
printf( "%#X\n", c );
14
printf( "\n%g\n", p );
15
printf( "%#g\n", p );
16
17
return 0;
18 }
Program Output:
02623
0x593
0X593
1427
1427.00
Dale Roberts
Example:
int i=1256;
printf(%d,i);
printf(%5d,i);
printf(%05d,i);
printf(%x,i);
printf(%-5d,i);
4
5
5
3
5
characters
characters
characters
characters
characters
1256
1256
01256
788
1256
Example:
float buf=125.12;
printf(%f,buf);
125.119995 (floating number precision error)
printf(%.0f,buf); 125
printf(%7.2f,buf); 125.12
printf(%07.2f,buf); 0125.12
Example:
char buf[] = hello, world;
printf(%10s,buf); hello, world
printf(%-10s,buf); hello, world
printf(%20s,buf); hello, world
printf(%20.10s,buf);
hello, wor
printf(%-20.10s,buf);
hello, wor
printf(%.10s,buf); hello, wor
Dale Roberts
Description
\?
\\
\a
\b
\f
\n
\r
\t
\v
Dale Roberts