函数参数的评估顺序 [英] Order of evaluation of function arguments
本文介绍了函数参数的评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码。
#include< stdio.h>
int main()
{
int i = 1;
printf("%d,%d,%d \ n",i,++ i,i ++);
返回0;
}
上面的代码是否定义良好?如果是,那么输出是什么。如果不是,为什么
?
谢谢。
解决方案
< blockquote> dragoncoder写道:
考虑以下代码。
#include< stdio.h>
int main()
{
int i = 1;
printf("%d,%d,%d \ n",i,++ i,i ++);
返回0;
}
上面的代码定义得很好吗?如果是,那么输出是什么。如果不是,为什么
?
这似乎是一个功课问题。如果是,请说明如下
并向我们展示你回答这个问题的最佳机会。
如果不是,请说服我们。
否则:阅读comp.lang.c常见问题解答。除了那个明显而有礼貌的事情_before_张贴第一个
时间之外,它应该可以帮助你回答这些问题
谢谢。
欢迎你。
干杯
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
dragoncoder写道:请考虑以下代码。
#include< stdio.h>
int main()
{
int i = 1;
printf("%d,%d,%d \ n",i,++ i,i ++);
返回0;
}
上面的代码定义得很好吗?如果是,那么输出是什么。如果不是,为什么
?
谢谢。
这不是一个功课问题。我只是想缩短它。我理解在两个序列之间多次修改值
点会调用未定义的行为,如
http://www.eskimo.com/~scs/C-faq/q3.2.html 但是这里的场景是
我相信不同。
我认为逗号(,)运算符是一个序列点,因此
不满足UB的序列点要求。我觉得必须按照评估的顺序来做
。
请把我指向正确的方向。
Consider the following code.
#include <stdio.h>
int main()
{
int i =1;
printf("%d ,%d ,%d\n",i,++i,i++);
return 0;
}
Is the above code well defined? If yes, what is the output. If no, why
?
Thanks.
解决方案
dragoncoder wrote:Consider the following code.
#include <stdio.h>
int main()
{
int i =1;
printf("%d ,%d ,%d\n",i,++i,i++);
return 0;
}
Is the above code well defined? If yes, what is the output. If no, why
?
This seems to be a homework question. If yes, please state so
and show us your best shot at answering the question.
If no, convince us.
Otherwise: Read the comp.lang.c FAQ. Apart from that being the
obvious and polite thing to do _before_ posting for the first
time, it should help you answering these question
Thanks.
You''re welcome.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
dragoncoder wrote:Consider the following code.
#include <stdio.h>
int main()
{
int i =1;
printf("%d ,%d ,%d\n",i,++i,i++);
return 0;
}
Is the above code well defined? If yes, what is the output. If no, why
?
Thanks.
This is not a homework question. I just wanted to make it short. I
understand that modifying the value more than once between 2 sequence
points invokes undefined behaviour as mentioned in
http://www.eskimo.com/~scs/C-faq/q3.2.html but here scenario is
different I believe.
I think the comma (,) operator is a sequence point and therefore the
sequence point requirement of UB is not satisfied. I feel it has to do
something with the order of evaluation of argements.
Please put me to right direction.
这篇关于函数参数的评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文