According to ISO C standard (ISO/IEC 9899), printf
function contains "%n
" format to count the number of output characters.
n: The argument shall be a pointer to signed integer into which is written the number of characters written to the output stream so far by this call to
fprintf
. No argument is converted, but one is consumed. If the conversion specification includes any flags, a field width, or a precision, the behavior is undefined.
Example code:
#include <stdio.h>
int main (void) {
int c1, c2;
printf ("hello%n, world%n\n", &c1, &c2);
printf ("% d\t% d\n", c1, c2);
return 0;
}
The C runtime library on both Windows (MSVCRT/UCRT) and Linux (GLIBC) supports "%n
" format in printf
function. (However, "%n
" format is disabled by default when using cl.exe to compile C program in Windows. A nonstandard function _set_printf_count_output
by Microsoft is required to enable "%n
" format. That is because cl.exe is a C++ compiler, function printf
is not in common use in C++ [In C++ std::cout
is used instead]. If cl.exe followed C standard, "%n
" format should be enabled by default and disabled by using _set_printf_count_output
function. In addition, Microsoft creates a nonstandard function printf_s
, in which "%n
" format is completely disabled)
Unlike MSVCRT/UCRT in Windows and GLIBC in Linux, The Bionic C runtime library in Android does not support "%n
" format at all, which parses "%n
" format only into two ordinary characters "%
" and "n
". Why "%n
" format is not parsed in ISO standard by Bionic C runtime library?