Why a warning of “control reaches end of non-void function” for the main function?

后端 未结 3 945
野性不改
野性不改 2021-02-18 17:25

I run the following C codes and got a warning: control reaches end of non-void function

int main(void) {}

Any suggestions?

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-18 18:09

    As an alternative to the obvious solution of adding a return statement to main(), you can use a C99 compiler (“gcc -std=c99” if you are using GCC).

    In C99 it is legal for main() not to have a return statement, and then the final } implicitly returns 0.

    $ gcc -c -Wall t.c
    t.c: In function ‘main’:
    t.c:20: warning: control reaches end of non-void function
    $ gcc -c -Wall -std=c99 t.c
    $ 
    

    A note that purists would consider important: you should not fix the warning by declaring main() as returning type void.

提交回复
热议问题