C语句的副作用和顺序点
再次向所有专家问好,我再次偶然发现了一些问题。
故事:
我读过一本书,提到了一个序列点,即 ;
是一个点,在它前进到下一个语句之前,应该评估它之前的所有副作用。为了使我的问题的上下文更清晰,我将编写一个简单的代码。
代码:
while (guess++ < 10)
{
printf("%d \n" , guests);
我的思考和问题:
1.)从上面的代码来看,while
语句测试条件guess++
guess++
10
是一个完整的表达式。所以在我看来,它不是一个语句,因为它不以 ;
结尾。
2.)由于使用了后缀递增运算符,因此猜测值在递增之前进行评估。
3.)书上提到,自增操作是在使用guess
变量进行关系操作后立即执行的,那么只有printf()
函数会执行它的操作责任。
4.)我的问题是,由于测试条件不以 ;
结尾,因此它不是一个语句。但是为什么在调用printf()
函数之前实现的增量操作,而不是在print()
函数之后实现,只是它被增量?
5.)也许这是一个附带问题,书中提到 while
是一个结构化语句,但为什么我没有看到尾随 ;
附加在它上面 while(测试条件);
。
6.)这听起来可能是一个愚蠢的问题,但是有时当我阅读其他人编写的一些源代码时,我会看到他们中的一些人将 while 循环的左大括号 {
与 while 循环放在同一行while()
,这会导致它像 while(testcondition){
。这是惯例还是有任何特殊原因这样做?
感谢您花时间阅读我的问题,非常感谢您的帮助。
Greetings to all the expert again , and again I stumble across a few questions.
The Story :
I read a book mentions that a sequence point which is ;
is a point where all the side effect before it should be evaluated before it advanced to the next statement.In order to make the context of my question clean , I will write a simple code.
The Code :
while (guess++ < 10)
{
printf("%d \n" , guests);
My Thinking and the Question:
1.)From the above code, the while
statement test condition guess++ < 10
is a full expression.So in my mindset it is not a statement because it doesn't end with a ;
.
2.)Since a postfix increment operator is used , therefore the guess
value evaluated before it is incremented.
3.)The book mention that the increment operation is carry out right after the guess
variable is used for the relational operation , then only the printf()
function would carry out its duty.
4.)My question is that , since the test condition doesn't end with a ;
, therefore it's not a statement . But why the increment operation implemented before printf()
function is called , but not after the print()
function only it is being incremented??
5.)Maybe this is a side question , the book mention that while
is a structured statement , but why didn't I see a trailing ;
is append on it while(testcondition);
.
6.)It might sounds like a silly question , but sometime when I read some source code written by others , I will saw some of them place the open braces {
of while loop on the same line with the while()
, which causes it to be like that while(testcondition){
. Is this a convention or is there any special reason for doing this??
Thanks for spending time reading my problems , your help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答问题1:while循环中括号之间的代码实际上是一个完整的表达式。来自 维基百科:
完整表达式的详细描述可以在 C faq 中找到:
需要注意的是,完整表达式与语句或分号标记无关。
那么让我们深入研究一下这个问题。修复你的代码片段我想出了这个:
它产生了这个输出:
这意味着评估将等同于这段代码:
问题 5 的答案可以在这个 stackoverflow 问题中找到:
在 C/C++ 中为什么 do while(表达);需要分号吗?
Answering question 1: The code between the brackets of a while loop is actually a full expression. From wikipedia:
A good description of a full expression can be found in the C faq:
It's important to note that a full expression has nothing to do with a statement or the semi-colon token.
So lets dig into this a little bit. Fixing up your code snippit I came up with this:
Which produces this output:
So that means that the evaluation would be equivalent to this code:
The answer to question 5 can be found in this stackoverflow question:
In C/C++ why does the do while(expression); need a semi colon?