开发者

C statement's side effect and sequence point

开发者 https://www.devze.com 2023-03-01 17:20 出处:网络
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 sho

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 a开发者_JS百科ppreciated.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消