Week 4 -Formatted InputOutput in C
Week 4 -Formatted InputOutput in C
COMPUTING II
• Together with the percent sign (%), these form conversion specifications:
– Rounding floating-point values to an indicated number of decimal
places
– Aligning columns of numbers at their decimal points
– right-aligning and left-aligning outputs
– Inserting literal characters at precise locations in a line of output
– Representing floating-point numbers in exponential format
– Representing unsigned integers in octal and hexadecimal format
– Displaying data with fixed-size field widths and precisions
A
This is a string
This is a string
This is also a string
-1
-12
-123
-1234
-12345
Flag Description
- (minus sign) Left-align the output within the specified field.
+ Display a plus sign preceding positive values and a minus sign preceding
negative values.
space 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 o.
#
Prefix 0x or 0X to the output value when used with the hexadecimal conversion
specifiers x or X.
#
Output:
1234567890123456789012345678901234567890
hello 7 a 1.230000
1234567890123456789012345678901234567890
hello 7 a 1.230000
Output:
786
-786
+786
-786
Output:
547
-547
02623
0x593
0X593
1427
1427.00
Output:
+00000452
000000452
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
9.10 Printing Literals and Escape
Sequences (1 of 2)
\f (new page or form feed) Move the cursor to the next logical page’s start.
\r (carriage return) Move the cursor to the beginning of the current line.
\t (horizontal tab) Move the cursor to the next horizontal tab position.
\v (vertical tab) Move the cursor to the next vertical tab position.
Scan set
Output:
Enter three floating-point numbers:
1.27987 1.27987e+03 3.38476e-06
Output:
Enter a string: Sunday
The input was
Output:
Enter string: ooeeooahah
The input was "ooeeooa"