int x;
printf("hello %n World\n", &x);
printf("%d\n", x);
Run Code Online (Sandbox Code Playgroud)
Ada*_*eld 15
它不是那么有用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
...
}
Run Code Online (Sandbox Code Playgroud)