Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the main function in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to write the main function in C language". In the daily operation, I believe that many people have doubts about how to write the main function in C language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt about how to write the main function in C language. Next, please follow the editor to study!

1. Main ()

The direct mian () does not return a value and has no input parameters. In practice, some compilers will display warnings and return a default value of int. However, it is possible in the C89 standard, because the return type is not declared, so the default value int is returned.

2. Void main ()

The form that beginners often use, but they don't know where the source is, and there are no traces of this form mentioned in documents such as C89/C99/C11. The return value of this writing is void, with no parameters. It is worth noting that once the declaration is void, it is not possible to get the exit status of the program after it exits. Therefore, the return value of void is not recommended.

3. Int main (void)

A more common way of writing, the formal parameter of this method is void, indicating that it cannot pass in any parameters when it is called, so it cannot get command-line arguments.

4. Int main ()

It looks no different from the third one above, but it's still a little different. In terms of the following example:

# includevoid test (); int main () {test (4); return 0;} void test (int a) {printf ("% d", a);} the program can still run normally. Although the test function is declared to have no input parameter, it can be called with or without any parameter.

5. Int main (int argc,char * argv [])

The int argc input parameter is the number of command line parameters, and the char * argv [] input parameter is the command line parameter array. This method of writing is also common and is mainly used for functions that need to get parameters from the command line.

6. Int main (int argc,char * argv [], char * envp [])

A way of writing that contains three parameters. In the upgraded version of the fifth above, int argc is the number of parameters on the command line, char * argv [] is an array of command parameters, and char * envp [] is an array of environment variables. Although there is only one more parameter than the sixth, the global variable environ can replace the role of envp, and you can use getenv or putenv to get or set environment variables, so it is not necessary to use this form. Most of this writing comes from the extension of the compiler.

There are so many ways to write it, so which one is right?

Consult the C89/C99/C11 standard document, which clearly fixed two ways of writing: int main (void) {/ *... * /} int main (int argc, char * argv []) {/ *... * /} so, other writing methods do not meet the standard, some are historical relics, some are compiler extensions, and some do not know where they were born.

So having said so, for the sake of the general portability of the code, it is recommended to use the form provided by the standard, or to use the two writing methods specified in the standard documentation. If a function determines that no arguments are passed in, then qualifying with void is a good choice. At this point, the study of "how to write main functions in C language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report