根据C17 7.21.6.2/8中fscanf
的规范:
除非规范中包含
isspace
,[
或c
指定符,否则将跳过输入的空白字符(由n
函数指定)
如果格式字符串包含%%
,则它是带有%
说明符的说明。不是[
,c
或n
,因此该标准似乎表明此处应跳过前导空格。
我的问题是:这是正确的解释,还是该标准中的缺陷?
我测试了两种不同的实现(带有MSVCRT stdio的mingw-w64和带有MinGW stdio的mingw-w64)。前者没有跳过前导空格,后者没有。
测试代码:
#include <stdio.h>
int main(void)
{
int a, r;
// Should be 1 according to standard; would be 0 if %% does not skip whitespace
r = sscanf("x %1", "x%% %d", &a);
printf("%d\n", r);
// Should always be 1
r = sscanf("x%1", "x%% %d", &a);
printf("%d\n", r);
}
答案 0 :(得分:5)
它应该跳过空格。
规范中有一个示例专门说明应跳过空格:
示例5通话:
app.post('/apple-pay', function(req, res, next) { // Set your secret key: remember to change this to your live secret key in production var stripe = require("stripe")("sk_test_xxx"); console.log('we got here....') // Token is created using Checkout or Elements! // Get the payment token ID submitted by the form: const token = req.body.token; console.log(req.body) // Using Express console.log('this is the Token...' + token) const charge = stripe.charges.create({ amount: 499, currency: 'usd', description: 'Example charge', source: token, }, function(err, charge) { // asynchronously called console.log(err) }); });
会将
#include <stdio.h> /* ... */ int n, i; n = sscanf("foo %bar 42", "foo%%bar%d", &i);
的值分配给n
,将1
的值分配给i
,因为两个输入字符都被跳过42
和%
转换说明符。