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

后端 未结 7 936
猫巷女王i
猫巷女王i 2020-12-20 11:11

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

相关标签:
7条回答
  • 2020-12-20 11:46

    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.

    0 讨论(0)
  • 2020-12-20 11:49

    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.

    0 讨论(0)
  • 2020-12-20 11:56

    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.

    0 讨论(0)
  • 2020-12-20 12:07

    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();
    
    0 讨论(0)
  • 2020-12-20 12:07

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

    0 讨论(0)
  • 2020-12-20 12:10

    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.

    0 讨论(0)
提交回复
热议问题