简体   繁体   中英

Function of main() in C programming

I am working in the C programming language. What is the function of main() ? What is void main() and int main() ?

It is the entry point of a C program. See here:

https://en.wikipedia.org/wiki/Entry_point#C_and_C++

What is the function of main() ?

It is the entry point of your program. That's the first function which is executed when you run your program.


What is the difference between void main() and int main() ?

  • The valid syntax for the main() function is:

     int main(void)

    It can also take arguments. See more here.

  • The second syntax is not valid :

     void main(void)

That's because your main() should return the exit status of your program.

Best answer of Brian-Bi :

  • void main() { ... } is wrong . If you're declaring main this way, stop. (Unless your code is running in a freestanding environment, in which case it could theoretically be correct.)
  • main() { ... } is acceptable in C89 ; the return type, which is not specified, defaults to int . However, this is no longer allowed in C99 . Therefore...
  • int main() { ... } is the best way to write main if you don't care about the program arguments. If you care about program arguments, you need to declare the argc and argv parameters too. You should always define main in this way. Omitting the return type offers no advantage in C89 and will break your code in C99 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM