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:
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()
andint 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.
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 inC89
; the return type, which is not specified, defaults toint
. 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 inC89
and will break your code inC99
.
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.