在printf中的多个赋值语句在C [英] multiple assignment statements in printf in c

查看:194
本文介绍了在printf中的多个赋值语句在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c\">Parameter调用用C 一个函数前评估顺序


谁能请帮助我理解code以下的输出:

  INT的main()
{
    int类型的= 35;
    的printf(%D%D%D,A - ,A,A = 20,++,A = 39);
    返回0;
}

输出:20 19 19 39 19

在分配是如何处理的

AP preciated洞察力(编译c)中的printf函数。


解决方案

这是不确定的行为。函数参数计算顺序在C标准未指定,因此,它可能发生在任何顺序。

人们已经给你你可以阅读上的评论一些链接。但在短期,有事情称为序列点。这些保证需要被执行之前的一切,被执行,然后该程序可以继续。两个控制顺序点之间,这些指令可以按任意顺序执行。

从C11的标准:

3.4.4:


  

      
  1. 明确的行为结果
      使用未指定的值,或其他行为,其中本标准规定的
      两个或两个以上的可能性,并规定了这是选择在没有任何进一步的要求
      例如


  2.   
  3. 示例的未指定的行为的一个例子是在该函数参数的计算顺序。


  4.   

6.5.2.2.10说:


  

有功能标志和实际的评估后的序列点
  论点,但实际的呼叫。
在调用函数中的每个评价(包括
  其它函数调用的)不是之前或之后,另有具体测序
   被调用的函数的主体的执行indeterminately相对于测序
  执行被调用函数的。


在换句话说,是一个函数的参数评价之间没有顺序点,因此它们可以在任何命令编译感觉就像评估。

要完成的答案,这也是不确定的行为,当你试图改变的值 A 双序列点之间多次。

6.5.2:


  

如果一个标物体上的副作用是相对于同一标量对象在任一个不同的副作用,或使用相同的标量的值的值计算unsequenced
  对象的行为是不确定的。如果有一个前pression的SUBEX pressions多个允许排序,该行为是未定义如果在任何排序中发生这样的unsequenced副作用。 84)


  
  

84)他的段落呈现不确定的语句前pressions如结果
       I = + I + 1; 结果
       A [I +] =我; 结果
  同时让结果
       I = I + 1; 结果
       A [我] =我;


Possible Duplicate:
Parameter evaluation order before a function calling in C

can anyone please help me understand the output of code below:

int main()
{
    int a=35;
    printf("%d %d %d %d %d",a--,a,a=20,a++,a=39);
    return 0;
}

output:20 19 19 39 19

appreciated insight on how assignment is handled(compiled) in c in printf function.

解决方案

This is unspecified behavior. The order of evaluation of function arguments is not specified in the C standard, and therefore it could happen in any order.

People have already given you some links in the comments you can read on. But in short, there are things called "sequence points". These ensure that everything that needed to be executed before, is executed, and then the program can continue. Between two sequence points, the instructions can execute in any order.

From the C11 standard:

3.4.4:

  1. unspecified behavior
    use of an unspecified value, or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance

  2. EXAMPLE An example of unspecified behavior is the order in which the arguments to a function are evaluated.

6.5.2.2.10 says

There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.

In other words, there is no sequence point between the evaluation of a function's arguments, therefore they may be evaluated in whichever order the compiler feels like.

To complete the answer, this is also undefined behavior, as you try to change the value of a multiple times between two sequence points.

6.5.2:

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)

84) his paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;
while allowing
i = i + 1;
a[i] = i;

这篇关于在printf中的多个赋值语句在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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