FWSCANF(3P)
FWSCANF(3P) β POSIX Programmer's Manual π
okay bestie, sit down, grab a strawberry π, because this is the POSIX Programmer's Manual page for the wide-character scanf girlies, and it is long. we're gonna get through it together, i promise π
π« PROLOG
housekeeping first, bestie π«Ά β this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so go consult the corresponding Linux manual page for the details on Linux behavior, ok?), or honestly the interface may just not be implemented on Linux at all. not every platform shows up to the function party π
β¨ NAME
fwscanf, swscanf, wscanf β convert formatted wide-character input πΈ
π SYNOPSIS
#include <stdio.h>
#include <wchar.h>
int fwscanf(FILE *restrict stream, const wchar_t *restrict format, ...);
int swscanf(const wchar_t *restrict ws,
const wchar_t *restrict format, ...);
int wscanf(const wchar_t *restrict format, ...);
π DESCRIPTION
first things first: the functionality on this reference page is aligned with the ISO C standard, no cap. any conflict between the requirements described here and the ISO C standard is unintentional β this volume of POSIX.1β2017 defers to the ISO C standard. periodt. β¨
π¦ the three functions, aka the trio
fwscanf()shall read from the named input stream.wscanf()shall read from the standard input streamstdin.swscanf()shall read from the wide-character stringws.
each function reads wide characters, interprets them according to a format, and stores the results in its arguments. each one expects, as arguments, a control wide-character string format (described below, don't worry, we get there π
) and a set of pointer arguments indicating where the converted input should be stored.
π¨ the result is undefined if there are insufficient arguments for the format. literally do not do that.
if the format is exhausted while arguments remain, the excess arguments are evaluated but are otherwise ignored. so they're just... there. vibing. π
π’ numbered arguments, the %n$ situation
conversions can be applied to the nth argument after format in the argument list, rather than to the next unused argument. in this case, the conversion specifier wide character % (see below) is replaced by the sequence "%n$", where n is a decimal integer in the range [1,{NL_ARGMAX}]. this feature provides for the definition of format wide-character strings that select arguments in an order appropriate to specific languages β very "i'll take that argument out of order, thank you" of it π.
in format wide-character strings containing the "%n$" form of conversion specifications, it is unspecified whether numbered arguments in the argument list can be referenced from the format wide-character string more than once.
the format can contain either form of a conversion specification β that is, % or "%n$" β but the two forms cannot normally be mixed within a single format wide-character string π«. the only exception to this is that %% or %* can be mixed with the "%n$" form. when numbered argument specifications are used, specifying the *N*th argument requires that all the leading arguments, from the first to the *(N-1)*th, are pointers.
π the radix character
the fwscanf() function in all its forms allows for detection of a language-dependent radix character in the input string, encoded as a wide-character value. the radix character is defined in the current locale (category LC_NUMERIC). in the POSIX locale, or in a locale where the radix character is not defined, the radix character shall default to a <period> ('.'). she's got range π
π§ what the format actually is
the format is a wide-character string composed of zero or more directives. each directive is composed of one of the following:
- one or more white-space wide characters (
<space>,<tab>,<newline>,<vertical-tab>, or<form-feed>); - an ordinary wide character (neither
'%'nor a white-space character); or - a conversion specification.
it is unspecified whether an encoding error occurs if the format string contains wchar_t values that do not correspond to members of the character set of the current locale and the specified semantics do not require that value to be processed by wcrtomb().
π anatomy of a conversion specification
each conversion specification is introduced by the '%' or by the character sequence "%n$", after which the following appear in sequence:
- an optional assignment-suppressing character
'*'. - an optional non-zero decimal integer that specifies the maximum field width.
- an optional assignment-allocation character
'm'. - an optional length modifier that specifies the size of the receiving object.
- a conversion specifier wide character that specifies the type of conversion to be applied. the valid conversion specifiers are described below.
π₯ how the directives actually run
the fwscanf() functions shall execute each directive of the format in turn. if a directive fails, as detailed below, the function shall return. failures are described as input failures (due to the unavailability of input bytes) or matching failures (due to inappropriate input). two whole failure flavors, we love options π
a directive composed of one or more white-space wide characters is executed by reading input until no more valid input can be read, or up to the first wide character which is not a white-space wide character, which remains unread.
a directive that is an ordinary wide character shall be executed as follows: the next wide character is read from the input and compared with the wide character that comprises the directive. if the comparison shows that they are not equivalent, the directive shall fail, and the differing and subsequent wide characters remain unread. similarly, if end-of-file, an encoding error, or a read error prevents a wide character from being read, the directive shall fail.
a directive that is a conversion specification defines a set of matching input sequences, as described below for each conversion wide character. a conversion specification is executed in the following steps:
- input white-space wide characters (as specified by
iswspace()) shall be skipped, unless the conversion specification includes a[,c, ornconversion specifier. (those three are the exception girlies, they do not skip β) - an item shall be read from the input, unless the conversion specification includes an
nconversion specifier wide character. an input item is defined as the longest sequence of input wide characters, not exceeding any specified field width, which is an initial subsequence of a matching sequence. the first wide character, if any, after the input item shall remain unread. if the length of the input item is zero, the execution of the conversion specification shall fail β this condition is a matching failure, unless end-of-file, an encoding error, or a read error prevented input from the stream, in which case it is an input failure. - except in the case of a
%conversion specifier, the input item (or, in the case of a%nconversion specification, the count of input wide characters) shall be converted to a type appropriate to the conversion wide character. if the input item is not a matching sequence, the execution of the conversion specification shall fail β this condition is a matching failure. unless assignment suppression was indicated by a'*', the result of the conversion shall be placed in the object pointed to by the first argument following theformatargument that has not already received a conversion result if the conversion specification is introduced by%, or in the *n*th argument if introduced by the wide-character sequence"%n$".
π¨ if this object does not have an appropriate type, or if the result of the conversion cannot be represented in the space provided, the behavior is undefined. be normal about your types please π
π«Ά the 'm' assignment-allocation character
the %c, %s, and %[ conversion specifiers shall accept an optional assignment-allocation character 'm', which shall cause a memory buffer to be allocated to hold the wide-character string converted, including a terminating null wide character.
in such a case, the argument corresponding to the conversion specifier should be a reference to a pointer value that will receive a pointer to the allocated buffer. the system shall allocate a buffer as if malloc() had been called. the application shall be responsible for freeing the memory after usage β yes, you, bestie, that's your job, no one else is coming to free it for you π§Ή
if there is insufficient memory to allocate a buffer, the function shall set errno to [ENOMEM] and a conversion error shall result. if the function returns EOF, any memory successfully allocated for parameters using assignment-allocation character 'm' by this call shall be freed before the function returns. she cleans up after herself, iconic π
πΈ length modifiers and their meanings
hhβ specifies that a followingd,i,o,u,x,X, ornconversion specifier applies to an argument with type pointer tosigned charorunsigned char.hβ specifies that a followingd,i,o,u,x,X, ornconversion specifier applies to an argument with type pointer toshortorunsigned short.l(ell) β specifies that a followingd,i,o,u,x,X, ornconversion specifier applies to an argument with type pointer tolongorunsigned long; that a followinga,A,e,E,f,F,g, orGconversion specifier applies to an argument with type pointer todouble; or that a followingc,s, or[conversion specifier applies to an argument with type pointer towchar_t. if the'm'assignment-allocation character is specified, the conversion applies to an argument with the type pointer to a pointer towchar_t.ll(ell-ell) β specifies that a followingd,i,o,u,x,X, ornconversion specifier applies to an argument with type pointer tolong longorunsigned long long.jβ specifies that a followingd,i,o,u,x,X, ornconversion specifier applies to an argument with type pointer tointmax_toruintmax_t.zβ specifies that a followingd,i,o,u,x,X, ornconversion specifier applies to an argument with type pointer tosize_tor the corresponding signed integer type.tβ specifies that a followingd,i,o,u,x,X, ornconversion specifier applies to an argument with type pointer toptrdiff_tor the corresponding unsigned type.Lβ specifies that a followinga,A,e,E,f,F,g, orGconversion specifier applies to an argument with type pointer tolong double.
π¨ if a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined. do not freestyle this π
π the conversion specifier wide characters
the following conversion specifier wide characters are valid:
dβ matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence ofwcstol()with the value10for thebaseargument. in the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer toint.iβ matches an optionally signed integer, whose format is the same as expected for the subject sequence ofwcstol()with0for thebaseargument. in the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer toint.
and bestie, we are SO not done β the rest of the conversion specifiers are literally right there, keep scrolling π
okay bestie we're mid-section and that's fine, the specifier tour continues, grab a snack π
oβ matches an optionally signed octal integer. Its format is the same as what you'd expect for the subject sequence ofwcstoul()when the base argument is8. If there's no size modifier hanging around, the app has to make sure the matching argument is a pointer tounsigned.uβ matches an optionally signed decimal integer, format same as the subject sequence ofwcstoul()with base10. No size modifier? Then the app shall ensure the corresponding argument is a pointer tounsigned.xβ matches an optionally signed hexadecimal integer, format same as the subject sequence ofwcstoul()with base16. No size modifier? Pointer tounsigned, app's job to guarantee it.a,e,f,gβ matches an optionally signed floating-point number, or infinity, or NaN. Format is the same as expected for the subject sequence ofwcstod(). No size modifier? The app shall ensure the corresponding argument is a pointer tofloat.
And listen β if the fwprintf() family of functions generates character string representations for infinity and NaN (that's a symbolic entity encoded in floating-point format, iykyk) to support IEEE Std 754β1985, then the fwscanf() family shall recognize them as input. no cap. β¨
sβ matches a sequence of non-white-space wide characters. If there's nol(ell) qualifier in the mix, characters from the input field get converted as if by repeated calls to thewcrtomb()function, with the conversion state described by anmbstate_tobject initialized to zero before the first wide character is converted. If the'm'assignment-allocation character is not specified, the app shall ensure the corresponding argument is a pointer to a character array big enough to accept the sequence and the terminating null character, which gets added automatically. Otherwise, the app shall ensure the corresponding argument is a pointer to a pointer to awchar_t.
If the l (ell) qualifier is present and the 'm' assignment-allocation character is not specified, the app shall ensure the corresponding argument is a pointer to an array of wchar_t big enough to accept the sequence and the terminating null wide character, which shall be added automatically. If the l (ell) qualifier is present and the 'm' assignment-allocation character is present, the app shall ensure the corresponding argument is a pointer to a pointer to a wchar_t. that's the whole menu, fr fr π«Ά
[β matches a non-empty sequence of wide characters from a set of expected wide characters. we call that the scanset. π If nol(ell) qualifier is present, wide characters from the input field get converted as if by repeated calls towcrtomb(), with the conversion state described by anmbstate_tobject initialized to zero before the first wide character is converted. If the'm'assignment-allocation character is not specified, the app shall ensure the corresponding argument is a pointer to a character array large enough to accept the sequence and the terminating null character, which shall be added automatically. Otherwise, the app shall ensure the corresponding argument is a pointer to a pointer to awchar_t.
If an l (ell) qualifier is present and the 'm' assignment-allocation character is not specified, the app shall ensure the corresponding argument is a pointer to an array of wchar_t large enough to accept the sequence and the terminating null wide character. If an l (ell) qualifier is present and the 'm' assignment-allocation character is specified, the app shall ensure the corresponding argument is a pointer to a pointer to a wchar_t.
Okay so here's how the scanlist actually works, pay attention bestie π
The conversion specification includes all subsequent wide characters in the format string up to and including the matching <right-square-bracket> (']'). The wide characters between the square brackets β that's the scanlist β make up the scanset, unless the wide character right after the <left-square-bracket> is a <circumflex> ('^'). In that case the scanset contains all the wide characters that don't appear in the scanlist between the <circumflex> and the <right-square-bracket>.
If the conversion specification begins with "[]" or "[^]", the <right-square-bracket> is included in the scanlist and the next <right-square-bracket> is the matching one that ends the conversion specification. Otherwise, the first <right-square-bracket> is the one that ends it. And if a '-' is in the scanlist and it's not the first wide character, nor the second (when the first wide character is a '^'), nor the last wide character, the behavior is implementation-defined. so like, don't be weird about the dash. π
cβ matches a sequence of wide characters of exactly the number specified by the field width (that's1if no field width shows up in the conversion specification).
If no l (ell) length modifier is present, characters from the input field shall be converted as if by repeated calls to the wcrtomb() function, with the conversion state described by an mbstate_t object initialized to zero before the first wide character is converted. No null character is added. If the 'm' assignment-allocation character is not specified, the app shall ensure the corresponding argument is a pointer to the initial element of a character array large enough to accept the sequence. Otherwise, the app shall ensure the corresponding argument is a pointer to a pointer to a char.
No null wide character is added. If an l (ell) length modifier is present and the 'm' assignment-allocation character is not specified, the app shall ensure the corresponding argument shall be a pointer to the initial element of an array of wchar_t large enough to accept the sequence. If an l (ell) qualifier is present and the 'm' assignment-allocation character is specified, the app shall ensure the corresponding argument is a pointer to a pointer to a wchar_t.
pβ matches an implementation-defined set of sequences, and it shall be the same set of sequences that gets produced by the%pconversion specification of the correspondingfwprintf()functions. The app shall ensure the corresponding argument is a pointer to a pointer tovoid. The interpretation of the input item is implementation-defined. If the input item is a value that was converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise, the behavior of the%pconversion is undefined. πnβ consumes no input. The app shall ensure the corresponding argument is a pointer to the integer into which is to be written the number of wide characters read from the input so far by this call to thefwscanf()functions. Executing a%nconversion specification shall not increment the assignment count returned at the completion of execution of the function. No argument shall be converted, but one shall be consumed. And if the conversion specification includes an assignment-suppressing wide character or a field width, the behavior is undefined. πCβ equivalent tolc.Sβ equivalent tols.%β matches a single'%'wide character; no conversion or assignment shall occur. The complete conversion specification shall be%%.
If a conversion specification is invalid, the behavior is undefined. π¨
The conversion specifiers A, E, F, G, and X are also valid, and they shall be equivalent to, respectively, a, e, f, g, and x.
If end-of-file is encountered during input, conversion is terminated. If end-of-file occurs before any wide characters matching the current conversion specification (except for %n) have been read β other than leading white-space, where that's permitted β execution of the current conversion specification shall terminate with an input failure. Otherwise, unless execution of the current conversion specification is terminated with a matching failure, execution of the following conversion specification (if any) shall be terminated with an input failure.
Reaching the end of the string in swscanf() shall be equivalent to encountering end-of-file for fwscanf().
If conversion terminates on a conflicting input, the offending input shall be left unread in the input. Any trailing white space (including <newline>) shall be left unread unless it's matched by a conversion specification. And the success of literal matches and suppressed assignments is only directly determinable via the %n conversion specification. that's the one and only receipt bestie π§Ύβ¨
The fwscanf() and wscanf() functions may mark the last data access timestamp of the file associated with stream for update. The last data access timestamp shall be marked for update by the first successful execution of fgetwc(), fgetws(), fwscanf(), getwc(), getwchar(), vfwscanf(), vwscanf(), or wscanf() using stream that returns data not supplied by a prior call to ungetwc().
β¨ RETURN VALUE β¨
Upon successful completion, these functions shall return the number of successfully matched and assigned input items. and yeah, that number can be zero if there was an early matching failure β zero is still a valid answer, no shame π«Ά
If the input ends before the first conversion (if any) has completed, and no matching failure has occurred, EOF shall be returned. If an error occurs before the first conversion (if any) has completed, and no matching failure has occurred, EOF shall be returned and errno shall be set to indicate the error. And if a read error occurs, the error indicator for the stream shall be set. π
that's a lot of EOF bestie, keep an eye on that errno. π
π₯ ERRORS π₯
okay so bestie, when things go wrong, here's the tea β β for the exact conditions where the fwscanf() functions shall fail and where they may fail, you gotta go peek at fgetwc(). that's the homework assignment, not me being lazy. π
on top of all that, fwscanf() shall fail if:
EILSEQβ the input byte sequence doesn't form a valid character. like, that's not a letter bestie, that's nonsense πENOMEMβ there's not enough storage space available. the memory said "i'm full, go away" π
and also, fwscanf() may fail if:
EINVALβ there are insufficient arguments. you promised me more args than you delivered, and that's a red flag π©
heads up: the sections from here on are informative β they're the vibes and the receipts, not the strict rules. πΈ
β¨ EXAMPLES β¨
okay so picture this call:
int i, n; float x; char name[50];
n = wscanf(L"%d%f%s", &i, &x, name);
and then you feed it this input line:
25 54.32E-1 Hamster
what happens? slay, that's what happens. n gets the value 3, i gets the value 25, x gets the value 5.432, and name contains the string "Hamster". absolutely ate that conversion, no crumbs. π₯
now another one, this time with some chaos:
int i; float x; char name[50];
(void) wscanf(L"%2d%f%*d %[0123456789]", &i, &x, name);
with this input:
56789 0123 56a72
the results: 56 gets assigned to i, 789.0 goes to x, 0123 gets skipped entirely (the * said "not today"), and the string "56\0" lands in name. and here's the little plot twist β the next call to getchar() shall return the character 'a'. that's right, it stops right there. main character energy from the letter a. π
π«Ά APPLICATION USAGE π«Ά
so in format strings that use the '%' form of conversion specifications, every single argument in the argument list gets used exactly once. no double dipping, no skipping. one arg, one use. periodt. π
and for functions that allocate memory as if by malloc(), the application should release that memory when it's no longer needed by calling free(). for fwscanf(), that's the memory that gets allocated when you use the 'm' assignment-allocation character. don't leave it hanging bestie, free your memory, it deserves closure π§
π RATIONALE π
None.
(yeah. nothing. the rationale said "no thoughts, head empty" and honestly? iconic.) β¨
π¦ FUTURE DIRECTIONS π¦
None.
(also nothing. we're thriving in the present, no notes.) π
π SEE ALSO π
go read these next, i promise they're relevant:
- Section 2.5, Standard I/O Streams
getwc()fwprintf()setlocale()wcstod()wcstol()wcstoul()wcrtomb()
and from the Base Definitions volume of POSIX.1β2017:
- Chapter 7, Locale
<inttypes.h><stdio.h><wchar.h>
π COPYRIGHT π
some of this text is reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. π
and just so we're clear: if this version and the original IEEE and The Open Group Standard ever disagree about anything, the original IEEE and The Open Group Standard is the referee document. it makes the calls, no arguments. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that show up in this page are most likely to have been introduced during the conversion of the source files into man page format. to report those errors, go here: https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's a wrap on the whole page, bestie π this one came from IEEE/The Open Group, dated 2017, and it's the FWSCANF(3P) page β so go forth, scan those wide strings, and stay hydrated πβ¨π¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms