C: why are extra semicolons OK?

前端 未结 7 858
长发绾君心
长发绾君心 2021-01-25 06:20
#include 

int main() {
    int a = -1, b = -10, c = 5;
    if (a > b)
        printf(\"Hello World\");
    else
        printf(\"Get out World\");;;;;         


        
相关标签:
7条回答
  • 2021-01-25 06:46

    ;;;;; are empty statements. They are allowed in C.

    0 讨论(0)
  • 2021-01-25 06:54

    As we know, semicolon (;) works as a statement terminator. In the Line no.8 the first semicolon terminates the statement under else and the next semicolon terminates the empty statement, and so on.

    0 讨论(0)
  • 2021-01-25 06:56

    An empty statement is legal in C. Since ; is the statement terminator, multiple ; are syntactically valid. Sometimes this is even useful: such as the for (;;) {/*code here*/} idiom.

    (Although some compilers will warn you in appropriate instances).

    Do note that, conceptually at least, the excess ; in that line are not part of the if block.

    0 讨论(0)
  • 2021-01-25 06:56

    Your program just has a sequence of empty statements at the end of the main function. It is parsed as this:

    #include <stdio.h>
    
    int main() {
        int a = -1, b = -10, c = 5;
    
        if (a > b)
            printf("Hello World");
        else
            printf("Get out World");
        ;
        ;
        ;
        ;
        ;
    }
    

    Note that int main() should be written int main(void) and it should return an int value, for example 0. C99 makes this return 0; implicit, but it is considered bad style and it is indeed less portable to omit it.

    0 讨论(0)
  • 2021-01-25 07:01

    Extra semicolons in general are not "OK".

    For example, changing your code slightly:

    #include <stdio.h>
    
    int main() {
        int a = -1, b = -10, c = 5;
        if (a > b)
            printf("Hello World");;;;;;
        else
            printf("Get out World");
    }
    

    That won't even compile.

    Another example, and this one does compile:

       ...
    
       int done = 0;
    
       ...
    
       while ( !done );
       {
           ...
    
           done = 1;
       }
    
       ...
    
    0 讨论(0)
  • 2021-01-25 07:02

    Well, the ; semicolon in C is a statement terminator, which means that you cannot put it where you like, only at the end of a statement.

    The null statement (one that does nothing) is valid in C, which allow you to write things as the one you post, and things like:

    while((data = do_read(...)) != NULL); /* skip input until EOF */
    

    that should be more visible written as (a single semicolon is too small to be skipped in a quick read):

    while((data = do_read(...)) != NULL) continue;
    

    which looks better as you'll never think that the next line is inside the loop.

    By the way, if you write something like:

    if (a > b); /* <====== LOOK at this semicolon */
        printf("Hello World");;;;;;
    else
        printf("Get out World");
    

    it will not compile, as that means if a greater than b just do nothing and that's a good valid sentence in C. By contrast you'll get an error several lines later, when the compiler gets to the else token and cannot match it to an if statement (remember, when you used the ;, you finished the ifstatement)

    That also happens to occur with the group of semicolons you put after the printf("Hello World"); statement (the first semicolon terminates it, and also as the next token is not an else but a ;, also terminates the if statement).

    The statements in the example above are:

    if (a > b); /* if a greater than b do nothing */
    ; /* do nothing */
    ; /* ... */
    ; /* ... */
    ; /* ... */
    ; /* do nothing */
    else /* ERROR, else cannot begin a statement */
    ....
    

    Here, as you got a syntax error from the compiler, you cannot have a good C program so you'll not get further analysing (the compiler tries to recover and continue parsing past that point, but you can have new errors originating of the new condition, or fake errors)

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