C语句的副作用和顺序点

发布于 2024-11-04 05:22:58 字数 1060 浏览 1 评论 0原文

再次向所有专家问好,我再次偶然发现了一些问题。

故事:

我读过一本书,提到了一个序列点,即 ; 是一个点,在它前进到下一个语句之前,应该评估它之前的所有副作用。为了使我的问题的上下文更清晰,我将编写一个简单的代码。

代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-11-11 05:22:58

回答问题1:while循环中括号之间的代码实际上是一个完整的表达式。来自 维基百科

此类别包括表达式语句(例如赋值 a=b;)、return 语句、if、switch、while 或 do-while 语句的控制表达式以及 for 语句中的所有三个表达式。

完整表达式的详细描述可以在 C faq 中找到:

完整表达
形成表达式语句的完整表达式,或者 if、switch、while、for 或 do/while 语句的控制表达式之一,或者初始值设定项或 return 语句中的表达式。完整表达式不是更大表达式的一部分。 (参见 ANSI 第 3.6 节或 ISO 第 6.6 节。)

需要注意的是,完整表达式与语句或分号标记无关。

那么让我们深入研究一下这个问题。修复你的代码片段我想出了这个:

#include <stdio.h>
int main(void)
{
    unsigned guess = 0;
    while (guess++ < 10)
    {
       printf("%d " , guess);
    }
    return 0;
}

它产生了这个输出:

1 2 3 4 5 6 7 8 9 10

这意味着评估将等同于这段代码:

while (guess < 10)
{
    guess++;
    printf("%d " , guess);
}

问题 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:

This category includes expression statements (such as the assignment a=b;), return statements, the controlling expressions of if, switch, while, or do-while statements, and all three expressions in a for statement.

A good description of a full expression can be found in the C faq:

full expression
The complete expression that forms an expression statement, or one of the controlling expressions of an if, switch, while, for, or do/while statement, or the expression in an initializer or a return statement. A full expression is not part of a larger expression. (See ANSI Sec. 3.6 or ISO Sec. 6.6.)

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:

#include <stdio.h>
int main(void)
{
    unsigned guess = 0;
    while (guess++ < 10)
    {
       printf("%d " , guess);
    }
    return 0;
}

Which produces this output:

1 2 3 4 5 6 7 8 9 10

So that means that the evaluation would be equivalent to this code:

while (guess < 10)
{
    guess++;
    printf("%d " , guess);
}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文