-2
#include<stdio.h>

int main()
{
    int i=5;
    while(i--)
    {
        printf("%d\n",i);
    };
    printf("Out of loop\n");
    return 0;
}

This code works fine! Does ; at the end of a loop not mean anything? So even if we add it, we don't have any problems? Just curious! I think adding ; at the end of a loop doesn't make any sense.

Joshua Snider
  • 705
  • 1
  • 8
  • 34
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    the extra `;` is the termination of an "empty statement". Adding `;;` after the `}` would add two empty statements. – wildplasser Dec 03 '14 at 10:57
  • Note that, in `do { /* code */ } while ( /* condition */ );`, the trailing `;` is *not* an empty statement, but required. – DevSolar Dec 03 '14 at 11:06

6 Answers6

10

It "works", but it's an empty statement so it changes the structure of your program.

Consider this:

if (foo())
  while (bar())
  {
    foo_some_more()
  }
else
{
  do_something_about_it();
}

The above works since the while is a single statement, and thus the else is still able to "find" the if properly. If you add a semi-colon after the while-loop's closing brace, you break that structure and it will no longer compile.

So, although empty statements can look harmless, they are not so they really should be avoided. Plus: they add pointless confusion to the code, of course.

unwind
  • 391,730
  • 64
  • 469
  • 606
6

The ; here is considered as an empty statement or null statement.

Sample

int i = 0;;

For more information, you can check this question.

Word of Caution: Don't consider this as a thumb rule. There are cases where the representation may appear same, but they are not null statement but part of a syntax. Example: do..while loop

Community
  • 1
  • 1
Natasha Dutta
  • 3,242
  • 21
  • 23
4

A superfluous ; is an empty statement and is redundant in this particular instance.

One instance where you're required to use an empty statement is in a switch to a final branch that does nothing:

switch (expression)
{
    case 1:
       /*Some statements here or empty*/
    case 2:
    ;  /*A statement is required between here and the closing brace
         An empty statement will suffice.*/
}

You ought to avoid using superfluous empty statements; they can emit bugs (particularly if you enclose your code in if statements during a refactoring effort), and older compilers will complain. Using excess semicolons in macros is particularly pernicious.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

These are Perfectly Valid, They are called Empty statements,They do nothing.

int main()
{
  ;
  ;
  ;
  ;
  {
  }
  return 0;
}
Mysterious Jack
  • 621
  • 6
  • 18
0

As stated by various users above, the relevant semi-colon is an empty statement and has no effect.

This however is a slightly different story:

#include <stdio.h>

int main()
{
    int i=5;    
    while(i--); //<--- Put the semi-colon here and see what you get.
    {
        printf("%d\n",i);
    }    
    printf("Out of loop\n");    
    return 0;   
}
Persixty
  • 8,165
  • 2
  • 13
  • 35
0
#include<stdio.h>

int main()
{

    int i=5;
    while(i--)
    {
        printf("%d\n",i);
    }/*blank statement*/;
    printf("Out of loop\n");
    return 0;
}

it's just a blank statement. above The braces itself tells end of the block so then it would treat the ; as blank stament

Rizier123
  • 58,877
  • 16
  • 101
  • 156