Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolon at the ends of if-statements and functions in C

Tags:

c

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.

like image 939
codingbear Avatar asked May 11 '09 05:05

codingbear


4 Answers

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.

like image 155
Jon Skeet Avatar answered Oct 19 '22 09:10

Jon Skeet


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();
like image 43
Francis Avatar answered Oct 19 '22 08:10

Francis


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

like image 26
Jorge Gajon Avatar answered Oct 19 '22 08:10

Jorge Gajon


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.

like image 21
Frederico Avatar answered Oct 19 '22 08:10

Frederico