C: why are extra semicolons OK?

前端 未结 7 983
长发绾君心
长发绾君心 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:56

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

    #include 
    
    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.

提交回复
热议问题