Search code examples
objective-cxcodesyntaxline-endings

Multi-semicolon Line Endings: Acceptable?


So, while I was working on some code for my next app update, I noticed something strange: You can put a very large amount of semicolons at the end of Objective-C statements and it will compile just fine! Heck, it runs the same as well. Why in the world does this work?


Solution

  • Semicolons are just used to end the current statement. Empty statements are permitted in C-like languages, for example:

    int len = 0;
    while(str[len++]); // count the length of a null-terminated string
    
    if (1) {} else { printf("Uh oh... this can't be happening!\n"); }
    

    They don't do anything.

    Placing an arbitrarily large number of semicolons at the end of the line is just an extreme case of this.