why there is semicolon after loop while();

后端 未结 4 420
天命终不由人
天命终不由人 2020-12-18 10:54

I am reading a code of my friend an I see this:

#include 
#include 

void main()
{
    char string1[125], string2 [10];
    in         


        
4条回答
  •  隐瞒了意图╮
    2020-12-18 11:00

    The ; is just a null statement, it is a no op but it it the body of the while loop. From the draft C99 standard section 6.8.3 Expression and null statements:

    A null statement (consisting of just a semicolon) performs no operations.

    and a while statement is defined as follows from section 6.8.5 Iteration statements:

    while ( expression ) statement

    So in this case the statement of the while loop is ;.

    The main effect of the while loop is here:

    string1[i++] == string2[j++]
            ^^^             ^^^
    

    So each iteration of the loop increments i and j until the whole condition:

    string1[i++] == string2[j++] &&string1[i-1] != 0 && string2[j-1] != 0
    

    evaluates to false.

提交回复
热议问题