main() function defined without return type gives warning

后端 未结 5 573
春和景丽
春和景丽 2020-12-11 12:24

This is my program:

main()
{ 
    printf(\"hello world\\n\");
}

I get this warning when compiling it:

function should retur         


        
5条回答
  •  -上瘾入骨i
    2020-12-11 13:17

    There are few things which you should take note of :

    1. The int is the main() function's return type. That means that the kind of value main() can return is an integer.
    2. main( ) was tolerated by the C90 compilers but not by C99 compilers which means its not a part of C99 standard anymore , so don't do this.
    3. void main() is not a standard form ,some compilers allow this, but none of the standards have ever listed it as an option. Therefore, compilers don't have to accept this form, and several don't. Again, stick to the standard form, and you won't run into problems if you move a program from one compiler to another.
    4. And one last thing , instead of writing main like this :

      int main() // here you are being silent about passing arguments to main , meaning it may or may not take arguments

    write like this :

    int main(void)// this specifies there are no arguments taken by main
    

    You might wanna look at the C99 standard for further details.

提交回复
热议问题