scanf的是保证不改变失败的价值? [英] Is scanf guaranteed to not change the value on failure?

查看:169
本文介绍了scanf的是保证不改变失败的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个 scanf函数家庭功能不匹配当前的说明,它是允许写入到那里将存储在成功的值存储?

在我的系统下面的输出 213 两次,但就是保证?

标准(C99或C11)语言似乎并没有明确规定,原来的值应保持不变(无论是不确定或不)。

 的#include<&stdio.h中GT;诠释的main()
{
    INT D = 213;    //匹配失败
    sscanf的(富,%D,和D);
    的printf(%d个\\ N,D);    //输入故障
    sscanf的(,%D,和D);
    的printf(%d个\\ N,D);
}


解决方案

在C11标准的相关部分(7.21.6.2,为的fscanf):


  

7的指令,这是一个转换规范定义了一组匹配的输入序列,按下面的每一个符描述。 A转换规范在下面的步骤执行:


  
  

8 [...]


  
  

9的输入项目是从流中读取,除非该规范包括n说明符。一输入项被定义为不超过任何指定的字段的宽度和其,或者是一个preFIX,匹配输入sequence.285)的第一个字符,如果有的话,将输入后的输入字符最长序列项目仍然未读。如果输入项的长度为零,则该指令的执行失败;这个条件是一个匹配失败,除非结束文件,编码错误,或从流读误差prevented输入,在这种情况下,它是一个输入故障。


  
  

10除在一%符,输入项目的情况下(或者,在一个%正指示的情况下,输入字符的计数)被转换成适合于该转换说明一个类型。如果输入项不是一个匹配序列,该指令的执行失败:这种情况是匹配失败。除非分配SUP pression被一个*所示,转换的结果是由下面的一个尚未接收到转换结果的格式参数的第一个参数放置在对象指向。 [...]


在我看来,将步和如果输入项的长度为零,该指令的执行失败表明,如果输入不匹配格式的说明,除pretation站任何转让该说明符之前已经发生。


在另一方面,关于引述的那些的第4清楚地表明,说明符到其中一个失败分配,再使用适当的事件的有序序列语言


  

4 fscanf函数执行依次格式的每个指令。当所有的指令都被执行,或者指令失败(详见下文),该函数返回。


If a scanf family function fails to match the current specifier, is it permitted to write to the storage where it would have stored the value on success?

On my system the following outputs 213 twice but is that guaranteed?

The language in the standard (C99 or C11) does not seem to clearly specify that the original value should remain unchanged (whether it was indeterminate or not).

#include <stdio.h>

int main()
{
    int d = 213;

    // matching failure
    sscanf("foo", "%d", &d);
    printf("%d\n", d);

    // input failure
    sscanf("", "%d", &d);
    printf("%d\n", d);
}

解决方案

The relevant part of the C11 standard is (7.21.6.2, for fscanf):

7 A directive that is a conversion specification defines a set of matching input sequences, as described below for each specifier. A conversion specification is executed in the following steps:

8 […]

9 An input item is read from the stream, unless the specification includes an n specifier. An input item is defined as the longest sequence of input characters which does not exceed any specified field width and which is, or is a prefix of, a matching input sequence.285) The first character, if any, after the input item remains unread. If the length of the input item is zero, the execution of the directive fails; 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.

10 Except in the case of a % specifier, the input item (or, in the case of a %n directive, the count of input characters) is converted to a type appropriate to the conversion specifier. If the input item is not a matching sequence, the execution of the directive fails: this condition is a matching failure. Unless assignment suppression was indicated by a *, the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. […]

To me, the words "step" and "If the length of the input item is zero, the execution of the directive fail" indicate that if the input does not match a specifier in the format, interpretation stops before any assignment for that specifier has occurred.


On the other hand, the subclause 4 about the ones quoted makes it clear that specifiers up to the failing one are assigned, again using language appropriate for ordered sequences of events:

4 The fscanf function executes each directive of the format in turn. When all directives have been executed, or if a directive fails (as detailed below), the function returns.

这篇关于scanf的是保证不改变失败的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆