printf()语句中的++和++ a,如下面的代码所示 [英] a++ and ++a in printf() statement as in the code below

查看:101
本文介绍了printf()语句中的++和++ a,如下面的代码所示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  
int main()
{
int a = 2 < /跨度>;
printf( %d,%d,a ++,++ a);
return 0 ;
}



这个输出是3,4

  #include   <   stdio.h  >  
int main()
{
int a = 2 ;
printf( %d,%d,++ a,a ++);
return 0 ;
}



这个输出是4,2

因为要先打印2要先打印2 case然后int i增加两次,所以4下面应该打印。执行时有printf的规则。

谢谢。

解决方案

没有严格定义评估C函数参数的顺序。

你可以得到第二个案例打印 3,2 和第一个打印 3,3 取决于使用的编译器。


需要学习的经验:



  1. 在C / C ++中,未指定评估函数参数的顺序。
  2. 如果表达式中的任何变量具有副作用,则它应该是该表达式中该变量的唯一实例。
  3. 如果您不确定函数名称是否实际上是宏名称,请不要在函数调用参数中执行任何副作用。



Things 在C / C ++中做:

 x = a [x] + x ++;  //  未指定的评估序列 
v [x] = ++ x; // 你知道哪个是第一次增加的吗?
v = max(x ++,+ + Y); // 双边效果如果max是一个宏:#define max(a,b)(a)> (b)?(a):( b)



干杯

Andi


在函数参数列表中获得带有'side effetct'值的方式取决于它们在调用准备期间的使用方式。

根据经验,考虑到C从右到左消耗参数结果你得到的正是你应该期待的:

- 获取最右边的参数并传递它,然后递增它(后增量)

- 获取下一个参数,预先增加然后传递作为参数。

这似乎解决了所有问题:),但事实并非如此:(

不是真的,因为这不是保证,事实上C11标准状态,在§6.5.2.2句子10,参数评估序列是不确定的。这个决定来自于考虑修复参数评估的顺序会有限制了代码的优化。即如果以不同的顺序评估参数将允许更好的代码优化,则可以完成。

影响点是程序员必须仔细考虑参数的所有副作用,并且最好的解决方案是从函数参数中没有副作用。 :(

NB我说过函数如何使用与过去常用的术语'push'相对的参数,因为实际的方法依赖于处理器:win64总是通过寄存器传递参数(__fastcall)。

#include<stdio.h>
int main()
{
    int a=2;
    printf("%d,%d",a++,++a);
    return 0;
}


the output of this is 3,4

#include<stdio.h>
int main()
{
    int a=2;
    printf("%d,%d",++a,a++);
    return 0;
}


the output of this is 4,2
as a is to be printed first 2 has to be printed in first case and then int i is incremented twice so 4 should be printed next.Is there any rule for printf while executing.
thanks.

解决方案

The order in which C function parameters are evaluated isn't strictly defined.
You can get the second case to print 3, 2 and the first to print 3, 3 depending on the compiler used.


Lessons to learn:


  1. In C/C++, the sequence of evaluating function arguments is unspecified.
  2. If any variable in an expression has a side effect, it should be the only instance of that variable in that expression.
  3. If you don't know for sure if function name is in fact a macro name, don't do any side effects in the function call arguments.


Things not to do in C/C++:

x = a[x] + x++; // unspecified sequence of evaluation
v[x] = ++x; // do you know for sure which is first incremented?
v = max(x++, ++y); // double side effect if "max" is a macro: #define max(a,b) (a)>(b)?(a):(b)


Cheers
Andi


The way you get values with 'side effetct' in a function parameter's list depends on how they are used during call preparation.
As a rule of thumb, considering that C consume parameters from right to left the result you got is exactly what you should have expected:
- Get rightmost parameter and pass it, then increment it (postincrement)
- Get next parameter, preincrement it then pass as parameter.
This seems to solve all the problems :) ,but is not true :(
Is not true because this is not guarantee, infact C11 standard states, at §6.5.2.2 sentence 10 that the parameters evaluation sequence is undefined. This decision come from the considration that fixing the order of evaluation of parameters would have limited the optimization of code. I.e. If evaluating the parameters in a different sequence would allow a better code optimization it can be done.
The impacting point is that all 'side effects' of parameters have to be carefully considered by the programmer, and as best solution is preferrable to have no 'side effects' from functions parameters. :(
N.B. I said how function consumes parameters opposed to the term 'pushed' generally used in past because the actual methodology is dependent from processor: win64 always pass parameters through registers (__fastcall).


这篇关于printf()语句中的++和++ a,如下面的代码所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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