
When multiple characters for non-null-terminated strings are read, integers are used for both the width specification and the buffer size. This example reads a single character: char c The characters are treated as single-byte values the first two characters are stored in ws, the second two are stored in ws, and so on. This example reads in a string of up to nine single-byte-wide characters and puts them in a double-byte-wide character buffer. The character width is single byte, but the function supports double-byte characters. The S format specifier means use the character width that's "opposite" the default width supported by the function. Wscanf_s(L"%9S", ws, (unsigned)_countof(ws)) In this example, the width of the buffer type doesn't match the width of the format specifier. The buffer size parameter describes the maximum number of characters, not bytes. Use a static cast to convert a size_t value to unsigned for 64-bit build configurations. The size parameter is of type unsigned, not size_t. When a token is too large to fit, nothing is written to the buffer unless there's a width specification. You can use a width specification field to ensure the token that's read in fits into the buffer. The buffer size includes the terminal null.

Scanf_s("%9s", s, (unsigned)_countof(s)) // buffer size is 10, width specification is 9 For example, if you're reading a string, the buffer size for that string is passed as follows: char s It immediately follows the pointer to the buffer or variable. The buffer size in characters is passed as an additional parameter. Specify the sizes for all c, C, s, S, or string control set parameters. Unlike scanf and wscanf, scanf_s and wscanf_s require you to specify buffer sizes for some parameters. The versions of these functions that have the _l suffix are identical, except they use the locale parameter instead of the current thread locale. scanf_s doesn't currently support input from a UNICODE stream. wscanf_s and scanf_s behave identically if the stream is opened in ANSI mode. Wscanf_s is a wide-character version of scanf_s the format argument to wscanf_s is a wide-character string. If copying occurs between strings that overlap, the behavior is undefined. Each argument must be a pointer to a variable type that corresponds to the type specifier in format. The scanf_s function reads data from the standard input stream, stdin, and writes it into argument. If execution is allowed to continue, scanf_s and wscanf_s return EOF and set errno to EINVAL.įor information about these and other error codes, see errno, _doserrno, _sys_errlist, and _sys_nerr. If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. The return value is EOF for an error, or if the end-of-file character or the end-of-string character is found in the first attempt to read a character.

A return value of 0 indicates no fields were assigned. The return value doesn't include fields that were read but not assigned. Returns the number of fields successfully converted and assigned. These versions of scanf, _scanf_l, wscanf, _wscanf_l have security enhancements, as described in Security Features in the CRT. Reads formatted data from the standard input stream.
