tags:

views:

1011

answers:

7

I just ran into some code that overuse semicolons, or use semicolon for different purposes that I am not aware of.

I found semicolons at the end of if-statements and at the end of functions. For instance:

  int main (int argc, char * argv[]) {
       // some code

       if (x == NULL) {
           // some code
       };  <-----

       // more code

       return 0;
  }; <---

It is compiling with cc, not gcc. What do those semicolons do? I'm assuming that there is no difference because the compiler would just consider it as empty statement.

+18  A: 

They do nothing. They're a sign of someone who doesn't understand the language terribly well, I suspect.

If this is source code you notionally "own", I would remove the code and try to have a gentle chat with the person who wrote it.

Jon Skeet
We used to see this quite a bit many years back when we had a team of programmers move from Turbo Pascal to C. In Pascal, you need the semi-colon after 'End' in most circumstances, and the programmers incorrectly brought the semi-colon with them. Once the habit forms, it sticks.
Shane MacLaughlin
+5  A: 

that's dummy statememt. You sample is identical to

if (x == NULL) {
 // some code
 do_something_here();
}

/* empty (dummy statement) here */ ;

// more code
some_other_code_here();
Francis
You gave a better example than you thought. "// some code" should be "// empty here" because it IS empty and because 0 dummy statements are as legally valid as 1 dummy statement.
Windows programmer
I'm following his example, that's why there's "some code" - he must have something in his real code.
Francis
+4  A: 

You are right, the compiler considers them empty statements. They are not needed, I guess the programmer somehow thought they were.

Jorge Gajon
+1  A: 

These semicolons are not needed (as you said, they are empty statements). Your code compiles with gcc, providing that 'x' is defined (check http://www.codepad.org). There's no reason why a C compiler would refuse to compile your code.

ASk
+1  A: 

The first semicolon (after the if-statement) is just an empty expression which does nothing. I fail to see any point of having it there.

The second semicolon (after the function) is an error since it is outside of any block of code. The compiler should give a warning.

Frederico
Nope. No warning with cc. That's why I thought this piece of code is bizarre.Maybe if I run this through lint, it would throw error/warnings.
bLee
I thought the C standard allowed external declarations with no contents. In other words, bizarre but legally bizarre. Maybe I'd better check again.
Windows programmer
Windows Programmer, it's C++ that allows an optional semicolon after a member function definition. But neither C nor C++ allow them to appear after a ordinary function definition, to my knowledge
Johannes Schaub - litb
I'm not sure, but section 6.7 "Declarations" of the C standard says "A declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration."I interpret that as if a semicolon alone doesn't count as a declaration.
Frederico
Furthermore, with Pedantic Warnings on, GCC says "ISO C does not allow extra ';' outside of a function.
Frederico
@Federico: I was talking about cc without any option. I am aware of the pedantic option, but I was just curious because I thought the compiler without any option should've taken care of this type of errors.
bLee
Federico's quotes clinch it for the second semicolon. Since the second semicolon isn't part of a valid declaration it must be a syntax error.
Windows programmer
A: 

I think that the author may have been going for something like:

if(condition for tbd block)
    ;
else {
    //Some code here
}

which you might do if you were scaffolding code and still wanted it to compile. There's a good chance that it's just an error as Jon suggests though.

Dana the Sane
A: 

These semicolons are useless as others have pointed out already. The only thing I want to add is that IMO, these are optimized out anyway i.e., compiler doesn't generate any real code for these.

Aamir