Topic 3 - Formatted Input/Output Flashcards
What is a conversion specification?
A placeholder.
What is %d used for
int values as a conversion specifier
What is %f used for
float values as a conversion specifier
Too many conversion specifications _____ produce meaningless output.
will
Too few conversion specifications _____ produce meaningless output
might
Incorrect specification ____ produce meaningless output
will
In the conversion specification form: %m.pX (or %-m.pX) - what is m?
minimum field width - specifies the minimum number of characters to print
What happens when the value to be printed requires fewer than m characters?
It will be right-justified within the field and space characters will be inserted to its left
What does putting a minis sign in front of m do?
causes left justification in the event that the value to be printed requires fewer than m characters. Space characters are then inserted to the right of the value instead.
What happens if the value to be printed requires more than m characters?
the field with automatically expands to the necessary size.
The main of p in %m.pX depends on what.
The choice of X, the conversion specifier.
What does “p” indicate for %d specifier?
The minimum number of digits to be displayed - extra zeros are added to the beginning of the number, if necessary
If p is omitted with %d, what happens.
P is assumed to be 1.
What does p indicate for %f?
%e?
%f - fixed decimal format - p indicates the number of digits after the decimal point
%e - exponential format - p as the same meaning as for the f specified
What does p indicate for %g?
%g - either exponential format or fixed decimal format, depending on the number to b printed - e format is selected in case of too small numbers
Does the %g conversion show trailing zeros?
No
If the number has no digits after the decimal point, does g display the decimal point?
No.
What does scanf do when it arrives at the final new-line?
It “peeks” at the final new-line without reading it.
How does scanf work when asked to read an integer?
- first looks for a plus / minus sign (optional), followed by
- digits
- it then reads digits until it reaches a non-digit
How does scanf work when asked to read a floating-point number?
- looks for plus or minus sign (optional)
- followed by digits (possibly containing a decimal point)
- it then reads digits until it reaches a non-digit
What happens when scanf encounters a character that can’t be part of the current item?
The character is “put back” to be read again during the scanning of the next input item
Scanf’s format string capabilities:
If the format string is “%d/%d” and the input is •5/•96¤
Does it fail or succeed?
The / from the input matches the / in the format string in the scanf’s arguments so it succeeds.
If the format string is “%d/%d” and the input is •5•/•96¤
Does it fail or succeed/
the “ / “ has a space character before the “/” that does not match the format string
If the format string is
“%d / %d” and the input is”
If the format string is “%d/%d” and the input is
•5/•96¤
5/96¤
•5•••••/•••••••96¤
5¤
/¤
96¤
- succeed
- succeed
- succeed
- succeed