int x;
printf("hello %n World\n", &x);
printf("%d\n", x);
它对于
printf()
来说没那么有用,但对于 sscanf()
来说却非常有用,特别是当您在多次迭代中解析字符串时。 fscanf()
和scanf()
会自动将其内部指针前进所读取的输入量,但sscanf()
不会。例如:
char stringToParse[256];
...
char *curPosInString = stringToParse; // start parsing at the beginning
int bytesRead;
while(needsParsing())
{
sscanf(curPosInString, "(format string)%n", ..., &bytesRead); // check the return value here
curPosInString += bytesRead; // Advance read pointer
...
}
取决于你所说的实用是什么意思。总是有其他方法可以实现它(例如,使用 s[n]printf 打印到字符串缓冲区并计算长度)。
但是
int len;
char *thing = "label of unknown length";
char *value = "value value value"
char *value2="second line of value";
printf ("%s other stuff: %n", thing, &len);
printf ("%s\n%*s, value, len, value2);
应该产生
label of unknown length other stuff: value value value
second line of value
(虽然未经测试,但我不靠近 C 编译器)
这作为一种对齐事物的方式非常实用,但我不想在代码中看到它。有更好的方法可以做到这一点。
这是相当深奥的。如果稍后需要替换生成的字符串中的占位符,您可能需要记住字符串中间的索引,这样您就不必保存原始 printf 参数或解析字符串。
它可以用来做恶行。
它可能用作获取各种子字符串长度的快速方法。
#include <stdio.h>
int main(int argc, char* argv[])
{
int col10 = (10 - 1);
int col25 = (25 - 1);
int pos1 = 0;
int pos2 = 0;
printf(" 5 10 15 20 25 30\n");
printf("%s%n%*s%n%*s\n", "fried",
&pos1, col10 - pos1, "green",
&pos2, col25 - pos2, "tomatos");
printf(" ^ ^ ^ ^ ^ ^\n");
printf("%d %d\n", pos1, pos2);
printf("%d %d\n", col10 - pos1, col25 - pos2);
return 0;
}
我肯定在这里遗漏了一些东西。西红柿离右边太远了。
你可以打电话
int _get_printf_count_output();
查看是否启用了 %n 支持,或使用
int _set_printf_count_output( int enable );
启用或禁用对 %n 格式的支持。
来自 MSDN VS2008
以下是 VS2005 CRT 代码中的一些内容:
/* if %n is disabled, we skip an arg and print 'n' */
if ( !_get_printf_count_output() )
{
_VALIDATE_RETURN(("'n' format specifier disabled", 0), EINVAL, -1);
break;
}
这带来了这个:
[死链接已删除]
对于以下行:
printf ("%s other stuff: %n", thing, &len);
我猜这主要是为了避免@eJames所说的内容。