是'scanf函数(QUOT;%D",&安培; X,&安培; X)`明确定义? [英] Is `scanf("%d%d", &x,&x)` well defined?

查看:153
本文介绍了是'scanf函数(QUOT;%D",&安培; X,&安培; X)`明确定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code以及界定?

 的#include<&stdio.h中GT;INT ScanFirstOrSecond(为const char * S,为int * DEST){
  返回的sscanf(s,%D,DEST,DEST);
}诠释主要(无效){
  INT X = 4;
  ScanFirstOrSecond(5,&放大器; X);
  的printf(%d个\\ N,X); //输出5  //这里是有点棘手
  ScanFirstOrSecond(6 7,&放大器; X);
  的printf(%d个\\ N,X); //输出7
  返回0;
}

在换句话说,不要在 ... 参数有一个隐含的限制来呢?

最适用C规格我发现的是


  

fscanf函数执行依次格式的每个指令。 ... C11dr§7.21.6.24



解决方案

看来C标准不指定的值存储在格式字符串的顺序。引用了C11标准:


  

7.21.6.2的的fscanf 函数


  
  

...


  
  

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


  
  

...


  
  

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


  
  

...


  
  

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


  
  

...


  
  

16 fscanf函数如果(如果有的话)已完成第一次转换之前发生输入故障返回宏EOF的值。否则,该函数返回分配的输入项目数,它可以是少于规定的,或者甚至是零,在早期匹配失败的情况下


无处本说明书中任何访问,甚至提到的输出对象。

YET 标准的措辞似乎表明,如果两个指针指向同一个对象,其行为可能是意外的:转换的结果存放在指向的对象通过下面的一个尚未收到转换结果的格式参数的第一个参数的这句话有些模棱两可:是什么的已尚未收到转换结果的参考?对象或参数?对象收到转换结果,而不是指针参数。在您的扭曲例如,对象 X 已经收到转换结果,所以它不应该得到一个又一个。

因此​​,似乎完全规定和明确的,但规范的措词可以完善以消除潜在的不确定性。

Is the following code well defined?

#include <stdio.h>

int ScanFirstOrSecond(const char *s, int *dest) {
  return sscanf(s, "%d%d", dest, dest);
}

int main(void) {
  int x = 4;
  ScanFirstOrSecond("5", &x);
  printf("%d\n", x);  // prints 5

  // Here is the tricky bit
  ScanFirstOrSecond("6 7", &x);
  printf("%d\n", x);  // prints 7
  return 0;
}

In other words, do the ... arguments have an implied restrict to them?

The most applicable C spec I found is

The fscanf function executes each directive of the format in turn. ... C11dr §7.21.6.2 4

解决方案

It seems the C Standard does specify that the values are stored in the order of the format string. Quoting the C11 Standard:

7.21.6.2 The fscanf function

...

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.

...

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:

...

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.

...

16 The fscanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

Nowhere else in this specification are any accesses to the output objects even mentioned.

YET the wording of the Standard seems to indicate that if 2 pointers point to the same object, the behavior might be unexpected: 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. This phrase is somewhat ambiguous: what does that has not already received a conversion result refer to? the object or the argument? Objects receive conversion results, not the pointer arguments. In your contorted example, the object x has already received a conversion result, so it should not receive another one.

So it seems fully specified and well defined, but the wording of the specification could be perfected to remove a potential ambiguity.

这篇关于是'scanf函数(QUOT;%D&QUOT;,&安培; X,&安培; X)`明确定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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