Starting from:

$24.99

CSC3320 Lab 7 – InLab Solution


Objective
2
 To learn how to print output using printf
 To learn how to get input using scanf
printf function
3
 Print output: e.g. printf(“%d”, x);
 printf function must be supplied with a format string as 1st argument.
E.g. “x has a value %d, y has a value %f”
%d and %f are type conversion code
Other characters are ordinary characters
print Function
4
printf(formatString, expr1, expr2, …);
Ordinary characters in a format string are
printed as they appear in the string
Conversion specifications are replaced by values of expressions in a sequential manner.
Example:
int i, j; float x, y;
i = 10; j = 20; x = 43.2892f; y = 5527.0f; printf("i =
%d, j = %d, x = %f, y = %f ", i, j, x, y+1);
Conversion Specifications
5
precision
width
%[-]m.pX

flags The type
conversion code.
Type Conversion Code
6

Type Type Type & Format
Conversion
Code
Integer %d (signed) int
%o int in octal
%x int in hexdecimal
%l long
%h short
Floating-point %f float
%lf double
Character %c char
String %s string stored in char []
Conversion Specifications - Width
7
%[-]m.pX
m: Specifies the minimum number of characters to print.
If the value has a width less than m, the field will be padded with spaces
Else, the value will not be truncated.
E.g. %10d will display an int right justified in a ten character space.
Conversion Specifications - Flags
8
%[-]m.pX
- : Putting a minus sign in front of m causes left justification; otherwise, only right justification
E.g. %-10d will display an int left justified in a ten character space.
Conversion Specifications
9
%[-]m.pX
. : Matches a decimal point.
p: Precision.
For floating-point number, it represents the number of characters used after the decimal.
E.g. %-10.3f will display a float using ten characters, with three digits after the decimal point.
More practice
11
Write down the output for the following statements.
(a) printf("%6d,%4d", 25, 1234);
␣␣␣␣25,1234
(b) printf("%.1f", 3.1415926);
3.1
(c) printf("%8.1f", 20.253);
␣␣␣␣20.3
(d) printf("%-10.5f", 20.253);
20.25300␣␣
scanf Function & must be used
for int, char,
12 double and float variables
 Get the input: e.g. scanf(“%d”, &x);
 In many cases, a scanf format string will contain only conversion specifications.
Example: scanf("%d%d%f%f", &i, &j, &x, &y);
The input as below
1
-20 .3
-4.0e3

How scanf work?
13
 scanf tries to match groups of input characters with conversion specifications in the format string.
 For each conversion specification, scanf tries to locate an item of the appropriate type in the input data, skipping blank space before it if necessary.
 scanf then reads the item, stopping when it reaches a character that can’t belong to the item.
 If the item was read successfully, scanf continues processing the rest of the format string.
scanf Function
14
Example: scanf("%d%d%f%f", &i, &j, &x, &y); The input is as below:
1
-20 .3
-4.0e3
scanf sees a stream of characters (¤ represents newline, ␣ represents a space):
␣␣1¤-20␣␣␣.3¤␣␣␣-4.0e3¤ ssrsrrrsssrrssssrrrrrr (s = skipped; r = read)
scanf“peeks” at the final new-line without reading it.
scanf Function
15
 A scanf format string can also contain literal strings in it.
 Then the input must match the literal string.
Example
%d/%d will match ␣5/␣96, but not ␣5␣/␣96
%d␣/%d will match ␣5␣/␣96

More products