I/O Format Specifiers for Reading Characters
Character Data — %c
Section titled “Character Data — %c”A character argument has a format specifier of the following form:
%[*] [width] c
| Modifier | Interpretation |
|---|---|
| Default Functionality | A character is read from the device and stored in the argument. |
* (asterisk) | An asterisk acts as the assignment suppression character. The input is scanned but not assigned to any parameters and is discarded. |
width | Specifies the number of characters to read and store in the argument. The default is 1. A NULL terminator is not appended to the argument. A # may be present in lieu of an integer width modifier, in which case an extra int argument supplies the value. |
Example — Single-character input
Section titled “Example — Single-character input”HRESULT hr = S_OK;
char ch;
// Read a single character from the device and store in the argumenthr = io.Scanf(_T("%c"), ch);
// Field width specifies 5 characters should be readchar sz[5];hr = io.Scanf(_T("%5c"), sz); // sz is NOT NULL terminated
// Field width specified using a variableint nWidth = 5;hr = io.Scanf(_T("%#c"), nWidth, sz); // sz is NOT NULL terminated
// Reads 5 characters from the device and discardshr = io.Scanf(_T("%*5c"));