Semicolon at the ends of if-statements and functio

2020-02-13 07:21发布

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.

标签: c
7条回答
够拽才男人
2楼-- · 2020-02-13 07:36

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

查看更多
该账号已被封号
3楼-- · 2020-02-13 07:37

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.

查看更多
劫难
4楼-- · 2020-02-13 07:38

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.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-13 07:39

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();
查看更多
闹够了就滚
6楼-- · 2020-02-13 07:45

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.

查看更多
Root(大扎)
7楼-- · 2020-02-13 07:55

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.

查看更多
登录 后发表回答