Emre İriş
Emre İriş

Reputation: 77

C ; operator usage

While I was writing code,C compiler (GCC in my case) doesn't give error or warning when using ; operator repeatedly like

int main()
{
  ;
  ;
  ;
  ;
  return 0;
}

It is obvious that for loop can be implemented without giving any input

for(;;)
{ /* Some implementations */
}

but when it comes to while loop, Compiler throws an error.

while()
{
}

So is it possible to explain what does actually ; operator do in C and why it does not give an error when it is put nothing before ;?

Upvotes: 1

Views: 116

Answers (3)

Lundin
Lundin

Reputation: 213458

; is not an operator, it is a punctuator that appears in various places in the language syntax. It may not appear arbitrary, but only in certain places specified by the formal syntax:

  • As a punctuator symbol inside a string literal or character constant. Example: ";".

  • At the end of a declaration or struct declaration. Example: int a;.

  • At the end of an expression statement. Example a=b;.
    This includes (6.8.3)

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

  • At the end of a Static_assert. Example _Static_assert(foo==bar, "foobar");.

  • At the end of a do-while statement. Example do(x) while(y) ;.

  • Separating the three clauses of a for statement. Example: for(int i=0; i<n; i++).

  • After a jump statement. Example return 0;.


The for loop is a bit of a special snowflake since it allows these two cases of formal grammar (6.8.5):

for ( expressionopt ; expressionopt ;expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement

Where opt means optional. Note that a declaration expression will contain a ; at the end, so it ends up with 2 of them no matter which version you pick.

Further, 6.8.5.3 states that the 1st and 3rd expressions are completely optional and can be omitted at any time. If the 2nd expression is omitted, it is however replaced by a non-zero constant, creating an eternal ("for ever") loop.

Normally such a loop is written as for(;;) {}.


Your examples are:

  • 4 null statement expressions.

  • An eternal for(;;) loop.

  • A syntax error since the grammar for while is:

    while ( expression ) statement.

    Where the expression is not optional.

Upvotes: 2

Adrian Mole
Adrian Mole

Reputation: 51825

The for loop with three 'empty' expressions (technically, the first is a clause) separated by the two semicolons works because each/any of those expressions is allowed to be omitted. From this C11 Draft Standard:

6.8.5.3 The for statement

1 The statement
for (clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows ...

2 Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

However, the while statement does not allow the controlling expression (inside the parentheses) to be omitted. From the same draft standard (note there is no 'paragraph 2'):

6.8.5.1 The while statement

1 The evaluation of the controlling expression takes place before each execution of the loop body

On the repeated use of the 'lone' semicolon(s) in your first code snippet: each of these delimits a null statement. Again, from the same draft standard:

6.8.3 Expression and null statements
...
3 A null statement (consisting of just a semicolon) performs no operations.

Null statements can be useful to define an 'empty' loop body; for example, the following code advances the i variable until a specific element in an array is found, but the loop itself doesn't have anything in the body:

int i;
char array[] = "abcdefghijklmnopqrstuvwxyz";
for (i = 0; array[i] != 'q'; ++i)
    ; // The ";" forms a null statement as the loop's body

// After the loop, "i" will be the index of the letter q

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 180103

So is it possible to explain what does actually ; operator do in C and why it does not give an error when it is put nothing before ;?

; is not an operator. It is a statement terminator, and, separately, it is part of the syntax of a for statement.

In its role as a statement terminator, ; can terminate an empty statement. C allows such statements, and specifies that they have no effect. This has limited use for providing an empty body for loop statements, in cases where the important work is done in the loop-control clause, but it is not limited to that context.

Similarly, in a for statement, any or all of the three expressions in the control clause are allowed to be omitted, but in that case the ; is still required to show which those are. If the controlling expression is omitted, then, by rule, it is treated as if it were a non-zero constant expression (that is, it is unconditionally truthy). That should be considered a special case. If the other expressions are omitted then simply no expression is evaluated at the corresponding points in the execution of the loop.

The rules for while statements, on the other hand, do not provide for the control expression to be omitted. The bottom line is that that's simply the rule.

Upvotes: 0

Related Questions